phonenumber.cpp 6.68 KB
Newer Older
1
/*
Christophe Giboudeaux's avatar
Christophe Giboudeaux committed
2
    This file is part of the KContacts framework.
3 4 5 6 7 8 9 10 11 12 13 14 15 16
    Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>

    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
Dirk Mueller's avatar
Dirk Mueller committed
17
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Dirk Mueller's avatar
Dirk Mueller committed
18
    Boston, MA 02110-1301, USA.
19 20
*/

21
#include "phonenumber.h"
22

23
#include <klocalizedstring.h>
24
#include <krandom.h>
25

Laurent Montel's avatar
Laurent Montel committed
26 27
#include <QDataStream>
#include <QSharedData>
28

Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
29
using namespace KContacts;
30

Laurent Montel's avatar
Laurent Montel committed
31
static QString cleanupNumber(const QString &input)
32
{
Laurent Montel's avatar
Laurent Montel committed
33
    return input.simplified();
34 35
}

36
class Q_DECL_HIDDEN PhoneNumber::Private : public QSharedData
37
{
Laurent Montel's avatar
Laurent Montel committed
38 39
public:
    Private(Type type)
Laurent Montel's avatar
Laurent Montel committed
40
        : mId(KRandom::randomString(8)), mType(type)
Tobias Koenig's avatar
Tobias Koenig committed
41 42
    {
    }
43

Laurent Montel's avatar
Laurent Montel committed
44 45
    Private(const Private &other)
        : QSharedData(other)
Tobias Koenig's avatar
Tobias Koenig committed
46
    {
Laurent Montel's avatar
Laurent Montel committed
47 48 49
        mId = other.mId;
        mType = other.mType;
        mNumber = other.mNumber;
Tobias Koenig's avatar
Tobias Koenig committed
50
    }
51

Laurent Montel's avatar
Laurent Montel committed
52
    QString mId;
Tobias Koenig's avatar
Tobias Koenig committed
53
    QString mNumber;
Laurent Montel's avatar
Laurent Montel committed
54
    Type mType;
55
    QMap<QString, QStringList> mParameters;
Tobias Koenig's avatar
Tobias Koenig committed
56 57 58
};

PhoneNumber::PhoneNumber()
Laurent Montel's avatar
Laurent Montel committed
59
    : d(new Private(Home))
60 61 62
{
}

Laurent Montel's avatar
Laurent Montel committed
63 64
PhoneNumber::PhoneNumber(const QString &number, Type type)
    : d(new Private(type))
65
{
Laurent Montel's avatar
Laurent Montel committed
66
    d->mNumber = cleanupNumber(number);
67 68
}

Laurent Montel's avatar
Laurent Montel committed
69 70
PhoneNumber::PhoneNumber(const PhoneNumber &other)
    : d(other.d)
71
{
Tobias Koenig's avatar
Tobias Koenig committed
72
}
73

Tobias Koenig's avatar
Tobias Koenig committed
74 75
PhoneNumber::~PhoneNumber()
{
76 77
}

Laurent Montel's avatar
Laurent Montel committed
78
bool PhoneNumber::operator==(const PhoneNumber &other) const
79
{
Laurent Montel's avatar
Laurent Montel committed
80 81 82
    if (d->mId != other.d->mId) {
        return false;
    }
Tobias Koenig's avatar
Tobias Koenig committed
83

Laurent Montel's avatar
Laurent Montel committed
84 85 86
    if (d->mNumber != other.d->mNumber) {
        return false;
    }
Tobias Koenig's avatar
Tobias Koenig committed
87

Laurent Montel's avatar
Laurent Montel committed
88 89 90
    if (d->mType != other.d->mType) {
        return false;
    }
91

92 93 94 95
    if (d->mParameters != other.d->mParameters) {
        return false;
    }

Laurent Montel's avatar
Laurent Montel committed
96
    return true;
97 98
}

Laurent Montel's avatar
Laurent Montel committed
99
bool PhoneNumber::operator!=(const PhoneNumber &other) const
Tobias Koenig's avatar
Tobias Koenig committed
100
{
Laurent Montel's avatar
Laurent Montel committed
101
    return !(other == *this);
Tobias Koenig's avatar
Tobias Koenig committed
102 103
}

Laurent Montel's avatar
Laurent Montel committed
104
PhoneNumber &PhoneNumber::operator=(const PhoneNumber &other)
Tobias Koenig's avatar
Tobias Koenig committed
105
{
Laurent Montel's avatar
Laurent Montel committed
106 107 108
    if (this != &other) {
        d = other.d;
    }
Tobias Koenig's avatar
Tobias Koenig committed
109

Laurent Montel's avatar
Laurent Montel committed
110
    return *this;
Tobias Koenig's avatar
Tobias Koenig committed
111 112 113
}

bool PhoneNumber::isEmpty() const
114
{
Laurent Montel's avatar
Laurent Montel committed
115
    return d->mNumber.isEmpty();
116
}
117

Laurent Montel's avatar
Laurent Montel committed
118
void PhoneNumber::setId(const QString &id)
119
{
Laurent Montel's avatar
Laurent Montel committed
120
    d->mId = id;
121 122 123 124
}

QString PhoneNumber::id() const
{
Laurent Montel's avatar
Laurent Montel committed
125
    return d->mId;
126 127
}

Laurent Montel's avatar
Laurent Montel committed
128
void PhoneNumber::setNumber(const QString &number)
129
{
Laurent Montel's avatar
Laurent Montel committed
130
    d->mNumber = cleanupNumber(number);
131 132 133 134
}

QString PhoneNumber::number() const
{
Laurent Montel's avatar
Laurent Montel committed
135
    return d->mNumber;
136 137
}

Laurent Montel's avatar
Laurent Montel committed
138
void PhoneNumber::setType(Type type)
139
{
Laurent Montel's avatar
Laurent Montel committed
140
    d->mType = type;
141 142
}

Tobias Koenig's avatar
Tobias Koenig committed
143
PhoneNumber::Type PhoneNumber::type() const
144
{
Laurent Montel's avatar
Laurent Montel committed
145
    return d->mType;
146
}
147

Tobias Koenig's avatar
 
Tobias Koenig committed
148 149
QString PhoneNumber::typeLabel() const
{
Laurent Montel's avatar
Laurent Montel committed
150
    return typeLabel(type());
Tobias Koenig's avatar
 
Tobias Koenig committed
151 152
}

Tobias Koenig's avatar
 
Tobias Koenig committed
153 154
PhoneNumber::TypeList PhoneNumber::typeList()
{
Laurent Montel's avatar
Laurent Montel committed
155
    static TypeList list;
156

Laurent Montel's avatar
Laurent Montel committed
157 158
    if (list.isEmpty()) {
        list << Home << Work << Msg << Pref << Voice << Fax << Cell << Video
159
             << Bbs << Modem << Car << Isdn << Pcs << Pager << Undefined;
Laurent Montel's avatar
Laurent Montel committed
160
    }
Tobias Koenig's avatar
 
Tobias Koenig committed
161

Laurent Montel's avatar
Laurent Montel committed
162
    return list;
Tobias Koenig's avatar
 
Tobias Koenig committed
163 164
}

Laurent Montel's avatar
Laurent Montel committed
165
QString PhoneNumber::typeFlagLabel(TypeFlag type)
166
{
Laurent Montel's avatar
Laurent Montel committed
167
    switch (type) {
168 169
    case Undefined:
        return i18nc("Undefined phone type", "Telephone number");
170
    case Home:
Laurent Montel's avatar
Laurent Montel committed
171
        return i18nc("Home phone", "Home");
172
    case Work:
Laurent Montel's avatar
Laurent Montel committed
173
        return i18nc("Work phone", "Work");
174
    case Msg:
Laurent Montel's avatar
Laurent Montel committed
175
        return i18n("Messenger");
176
    case Pref:
Laurent Montel's avatar
Laurent Montel committed
177
        return i18nc("Preferred phone", "Preferred");
178
    case Voice:
Laurent Montel's avatar
Laurent Montel committed
179
        return i18n("Voice");
180
    case Fax:
Laurent Montel's avatar
Laurent Montel committed
181
        return i18n("Fax");
182
    case Cell:
Laurent Montel's avatar
Laurent Montel committed
183
        return i18nc("Mobile Phone", "Mobile");
184
    case Video:
Laurent Montel's avatar
Laurent Montel committed
185
        return i18nc("Video phone", "Video");
186
    case Bbs:
Laurent Montel's avatar
Laurent Montel committed
187
        return i18n("Mailbox");
188
    case Modem:
Laurent Montel's avatar
Laurent Montel committed
189
        return i18n("Modem");
190
    case Car:
Laurent Montel's avatar
Laurent Montel committed
191
        return i18nc("Car Phone", "Car");
192
    case Isdn:
Laurent Montel's avatar
Laurent Montel committed
193
        return i18n("ISDN");
194
    case Pcs:
Laurent Montel's avatar
Laurent Montel committed
195
        return i18n("PCS");
196
    case Pager:
Laurent Montel's avatar
Laurent Montel committed
197
        return i18n("Pager");
198
    default:
Laurent Montel's avatar
Laurent Montel committed
199 200
        return i18nc("another type of phone", "Other");
    }
201
}
Tobias Koenig's avatar
 
Tobias Koenig committed
202

Laurent Montel's avatar
Laurent Montel committed
203
QString PhoneNumber::typeLabel(Type type)
204
{
Laurent Montel's avatar
Laurent Montel committed
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
    QString label;
    bool first = true;

    // special cases
    // Pref stand alone -> Preferred Number
    // Home+Fax or Work+Fax -> combine as initial string
    if (type == Pref) {
        return i18n("Preferred Number");
    }

    if (type & Fax) {
        if (type & Home) {
            label = i18n("Home Fax");
            first = false;
            type &= ~Fax;
            type &= ~Home;
        } else if (type & Work) {
            label = i18n("Work Fax");
            first = false;
            type &= ~Fax;
            type &= ~Work;
        }
227
    }
Allen Winter's avatar
Allen Winter committed
228

Laurent Montel's avatar
Laurent Montel committed
229
    const TypeList list = typeList();
230

Laurent Montel's avatar
Laurent Montel committed
231
    TypeList::ConstIterator it;
Laurent Montel's avatar
Laurent Montel committed
232 233
    TypeList::ConstIterator end(list.constEnd());
    for (it = list.constBegin(); it != end; ++it) {
Laurent Montel's avatar
Laurent Montel committed
234 235 236 237 238 239
        // these are actually flags
        const TypeFlag flag = static_cast<TypeFlag>(static_cast<int>(*it));
        if (type & flag) {
            if (!first) {
                label.append(QLatin1Char('/'));
            }
240

Laurent Montel's avatar
Laurent Montel committed
241
            label.append(typeFlagLabel(flag));
242

Laurent Montel's avatar
Laurent Montel committed
243 244 245 246
            if (first) {
                first = false;
            }
        }
247 248
    }

Laurent Montel's avatar
Laurent Montel committed
249
    return label;
250 251
}

Tobias Koenig's avatar
Tobias Koenig committed
252 253
QString PhoneNumber::toString() const
{
Laurent Montel's avatar
Laurent Montel committed
254
    QString str = QLatin1String("PhoneNumber {\n");
255 256
    str += QStringLiteral("    Id: %1\n").arg(d->mId);
    str += QStringLiteral("    Type: %1\n").arg(typeLabel(d->mType));
257 258
    if (!d->mParameters.isEmpty()) {
        QString param;
Laurent Montel's avatar
Laurent Montel committed
259 260 261 262
        QMap<QString, QStringList>::const_iterator it = d->mParameters.constBegin();
        const QMap<QString, QStringList>::const_iterator end = d->mParameters.constEnd();
        while (it != end) {
            param += QStringLiteral("%1 %2").arg(it.key(), it.value().join(QLatin1Char(',')));
263
            ++it;
264 265 266
        }
        str += QStringLiteral("    parameters: %1\n").arg(param);
    }
267 268
    str += QStringLiteral("    Number: %1\n").arg(d->mNumber);
    str += QLatin1String("}\n");
Tobias Koenig's avatar
Tobias Koenig committed
269

Laurent Montel's avatar
Laurent Montel committed
270
    return str;
Tobias Koenig's avatar
Tobias Koenig committed
271 272
}

273 274 275 276 277 278 279 280 281 282
void PhoneNumber::setParameters(const QMap<QString, QStringList> &params)
{
    d->mParameters = params;
}

QMap<QString, QStringList> PhoneNumber::parameters() const
{
    return d->mParameters;
}

Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
283
QDataStream &KContacts::operator<<(QDataStream &s, const PhoneNumber &phone)
Tobias Koenig's avatar
 
Tobias Koenig committed
284
{
285
    return s << phone.d->mId << (uint)phone.d->mType << phone.d->mNumber << phone.d->mParameters;
Tobias Koenig's avatar
 
Tobias Koenig committed
286 287
}

Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
288
QDataStream &KContacts::operator>>(QDataStream &s, PhoneNumber &phone)
Tobias Koenig's avatar
 
Tobias Koenig committed
289
{
Laurent Montel's avatar
Laurent Montel committed
290
    uint type;
291
    s >> phone.d->mId >> type >> phone.d->mNumber >> phone.d->mParameters;
Laurent Montel's avatar
Laurent Montel committed
292
    phone.d->mType = PhoneNumber::Type(type);
Tobias Koenig's avatar
 
Tobias Koenig committed
293

Laurent Montel's avatar
Laurent Montel committed
294
    return s;
Tobias Koenig's avatar
 
Tobias Koenig committed
295
}