phonecomboboxtype.cpp 3.43 KB
Newer Older
1 2 3 4
/*
    This file is part of Akonadi Contact.

    Copyright (C) 2016 eyeOS S.L.U., a Telefonica company, sales@eyeos.com
Laurent Montel's avatar
Laurent Montel committed
5
    Copyright (C) 2016-2017 Laurent Montel <laurent.montel@kdab.com>
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

    This library is free software; you can redistribute it and/or modify it
    under the terms of the GNU Library General Public License as published by
    the Free Software Foundation; either version 2 of the License, or (at your
    option) any later version.

    This library is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
    License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to the
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
*/

#include "phonecomboboxtype.h"
Laurent Montel's avatar
Laurent Montel committed
24
#include "phonetypedialog.h"
25
#include <KLocalizedString>
Laurent Montel's avatar
Laurent Montel committed
26
#include <autoqpointer_p.h>
27 28 29 30

using namespace Akonadi;

PhoneComboBoxType::PhoneComboBoxType(QWidget *parent)
Laurent Montel's avatar
Laurent Montel committed
31
    : Akonadi::AkonadiContactComboBox(parent)
32 33 34 35 36 37
    , mType(KContacts::PhoneNumber::Home)
    , mLastSelected(0)
{
    const int nbMax = KContacts::PhoneNumber::typeList().count();
    mTypeList.reserve(nbMax + 1);
    for (int i = 0; i < nbMax; ++i) {
Laurent Montel's avatar
Laurent Montel committed
38 39 40 41
        const KContacts::PhoneNumber::TypeFlag typeflag = KContacts::PhoneNumber::typeList().at(i);
        if (typeflag != KContacts::PhoneNumber::Pref) {
            mTypeList.append(typeflag);
        }
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
    }

    mTypeList.append(-1);   // Others...

    update();

    connect(this, static_cast<void (PhoneComboBoxType::*)(int)>(&PhoneComboBoxType::activated), this, &PhoneComboBoxType::selected);
}

PhoneComboBoxType::~PhoneComboBoxType()
{
}

void PhoneComboBoxType::setType(KContacts::PhoneNumber::Type type)
{
Laurent Montel's avatar
Laurent Montel committed
57
    if (static_cast<int>(type) != 0) {
58

Laurent Montel's avatar
Laurent Montel committed
59 60 61 62
        if (!mTypeList.contains(type)) {
            mTypeList.insert(mTypeList.at(mTypeList.count() - 1), type);
        }
    }
63 64 65 66 67 68 69 70 71
    mType = type;
    update();
}

KContacts::PhoneNumber::Type PhoneComboBoxType::type() const
{
    return mType;
}

Laurent Montel's avatar
Laurent Montel committed
72
void PhoneComboBoxType::resetToDefault()
Laurent Montel's avatar
Laurent Montel committed
73 74 75 76
{
    setCurrentIndex(0);
}

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
void PhoneComboBoxType::update()
{
    clear();

    const int typeListCount(mTypeList.count());
    for (int i = 0; i < typeListCount; ++i) {
        if (mTypeList.at(i) == -1) {     // "Other..." entry
            addItem(i18nc("@item:inlistbox Category of contact info field", "Other..."));
        } else {
            addItem(KContacts::PhoneNumber::typeLabel(KContacts::PhoneNumber::Type(mTypeList.at(i))));
        }
    }

    setCurrentIndex(mLastSelected = mTypeList.indexOf(mType));
}

void PhoneComboBoxType::selected(int pos)
{
    if (mTypeList.at(pos) == -1) {
        otherSelected();
    } else {
        mType = KContacts::PhoneNumber::Type(mTypeList.at(pos));
        mLastSelected = pos;
    }
}

void PhoneComboBoxType::otherSelected()
{
Laurent Montel's avatar
Laurent Montel committed
105
    AutoQPointer<Akonadi::PhoneTypeDialog> dlg = new Akonadi::PhoneTypeDialog(mType, this);
106 107 108
    if (dlg->exec()) {
        mType = dlg->type();
        if (!mTypeList.contains(mType)) {
Laurent Montel's avatar
Laurent Montel committed
109 110 111
            if (static_cast<int>(mType) != 0) {
                mTypeList.insert(mTypeList.at(mTypeList.count() - 1), mType);
            }
112 113 114 115 116 117 118
        }
    } else {
        setType(KContacts::PhoneNumber::Type(mTypeList.at(mLastSelected)));
    }

    update();
}