| 1 |
/* |
| 2 |
* Copyright (C) 2012-2014 Canonical, Ltd. |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation; version 3. |
| 7 |
* |
| 8 |
* This program is distributed in the hope that it will be useful, |
| 9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
* GNU General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU General Public License |
| 14 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include "contacts.h" |
| 18 |
#include "imagescalethread.h" |
| 19 |
|
| 20 |
#include <QtCore/QCoreApplication> |
| 21 |
#include <QtCore/QStringList> |
| 22 |
#include <QtCore/QDebug> |
| 23 |
#include <QtCore/QDir> |
| 24 |
#include <QtCore/QUrl> |
| 25 |
#include <QtCore/QLockFile> |
| 26 |
#include <QtCore/QThreadPool> |
| 27 |
|
| 28 |
#include "config.h" |
| 29 |
|
| 30 |
LomiriContacts::LomiriContacts(QObject *parent) |
| 31 |
: QObject(parent), |
| 32 |
m_fileWatcher(new QFileSystemWatcher) |
| 33 |
{ |
| 34 |
// We need to monitor the tmp dir since the file could not exists at this point |
| 35 |
m_fileWatcher->addPath(QDir::tempPath()); |
| 36 |
connect(m_fileWatcher.data(), |
| 37 |
SIGNAL(directoryChanged(QString)), |
| 38 |
SIGNAL(updateIsRunningChanged())); |
| 39 |
connect(m_fileWatcher.data(), |
| 40 |
SIGNAL(fileChanged(QString)), |
| 41 |
SIGNAL(updateIsRunningChanged())); |
| 42 |
} |
| 43 |
|
| 44 |
QString LomiriContacts::tempPath() const |
| 45 |
{ |
| 46 |
return QDir::tempPath(); |
| 47 |
} |
| 48 |
|
| 49 |
QString LomiriContacts::contactInitialsFromString(const QString &value) |
| 50 |
{ |
| 51 |
if (value.isEmpty() || !value.at(0).isLetter()) { |
| 52 |
return QString(); |
| 53 |
} |
| 54 |
|
| 55 |
QString initials; |
| 56 |
QStringList parts = value.split(" "); |
| 57 |
initials = parts.first().at(0).toUpper(); |
| 58 |
if (parts.size() > 1) { |
| 59 |
initials += parts.last().at(0).toUpper(); |
| 60 |
} |
| 61 |
|
| 62 |
return initials; |
| 63 |
} |
| 64 |
|
| 65 |
QString LomiriContacts::normalized(const QString &value) |
| 66 |
{ |
| 67 |
QString s2 = value.normalized(QString::NormalizationForm_D); |
| 68 |
QString out; |
| 69 |
|
| 70 |
for (int i=0, j=s2.length(); i<j; i++) |
| 71 |
{ |
| 72 |
// strip diacritic marks |
| 73 |
if (s2.at(i).category() != QChar::Mark_NonSpacing && |
| 74 |
s2.at(i).category() != QChar::Mark_SpacingCombining) { |
| 75 |
out.append(s2.at(i)); |
| 76 |
} |
| 77 |
} |
| 78 |
return out; |
| 79 |
} |
| 80 |
|
| 81 |
QString LomiriContacts::copyImage(const QUrl &imageUrl) |
| 82 |
{ |
| 83 |
ImageScaleThread *imgThread = new ImageScaleThread(imageUrl, this); |
| 84 |
QThreadPool::globalInstance()->start(imgThread); |
| 85 |
return imgThread->id(); |
| 86 |
} |
| 87 |
|
| 88 |
bool LomiriContacts::containsLetters(const QString &value) |
| 89 |
{ |
| 90 |
foreach (const QChar &c, value) { |
| 91 |
if (c.isLetter()) { |
| 92 |
return true; |
| 93 |
} |
| 94 |
} |
| 95 |
return false; |
| 96 |
} |
| 97 |
|
| 98 |
bool LomiriContacts::removeFile(const QUrl &file) |
| 99 |
{ |
| 100 |
QString localFile = file.toLocalFile(); |
| 101 |
if (!localFile.isEmpty() && QFile::exists(localFile)) { |
| 102 |
return QFile::remove(localFile); |
| 103 |
} |
| 104 |
return false; |
| 105 |
} |
| 106 |
|
| 107 |
bool LomiriContacts::updateIsRunning() const |
| 108 |
{ |
| 109 |
return QFile::exists(updaterLockFile()); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
QUrl LomiriContacts::tempFile(const QString &templateName) |
| 114 |
{ |
| 115 |
QTemporaryFile tmp(QString("%1/%2").arg(tempPath()).arg(templateName)); |
| 116 |
tmp.setAutoRemove(false); |
| 117 |
if (tmp.open()) |
| 118 |
return QUrl::fromLocalFile(tmp.fileName()); |
| 119 |
else |
| 120 |
return QUrl(); |
| 121 |
} |
| 122 |
|
| 123 |
uint LomiriContacts::qHash(const QString &str) |
| 124 |
{ |
| 125 |
return ::qHash(str); |
| 126 |
} |
| 127 |
|
| 128 |
QString LomiriContacts::updaterLockFile() |
| 129 |
{ |
| 130 |
return QString("%1/%2").arg(QDir::tempPath()).arg("/address-book-updater.lock"); |
| 131 |
} |