| 1 |
/* |
| 2 |
* Copyright (C) 2015 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 "simcardcontacts.h" |
| 18 |
|
| 19 |
#include <QDebug> |
| 20 |
#include <QDBusConnection> |
| 21 |
#include <qofonophonebook.h> |
| 22 |
|
| 23 |
SimCardContacts::SimCardContacts(QObject *parent) |
| 24 |
: QObject(parent), |
| 25 |
m_ofonoManager(new QOfonoManager(this)), |
| 26 |
m_dataFile(0) |
| 27 |
{ |
| 28 |
onManagerChanged(); |
| 29 |
m_modemsChangedTimer.setInterval(1000); |
| 30 |
m_modemsChangedTimer.setSingleShot(true); |
| 31 |
connect(m_ofonoManager.data(), |
| 32 |
SIGNAL(modemsChanged(QStringList)), |
| 33 |
SLOT(onManagerChanged()), |
| 34 |
Qt::QueuedConnection); |
| 35 |
connect(m_ofonoManager.data(), |
| 36 |
SIGNAL(availableChanged(bool)), |
| 37 |
SLOT(onManagerChanged()), |
| 38 |
Qt::QueuedConnection); |
| 39 |
connect(&m_modemsChangedTimer, |
| 40 |
SIGNAL(timeout()), |
| 41 |
SLOT(onModemsChanged())); |
| 42 |
} |
| 43 |
|
| 44 |
SimCardContacts::~SimCardContacts() |
| 45 |
{ |
| 46 |
Q_FOREACH(QOfonoModem *m, m_availableModems) { |
| 47 |
disconnect(m); |
| 48 |
m->deleteLater(); |
| 49 |
} |
| 50 |
m_availableModems.clear(); |
| 51 |
|
| 52 |
cancel(); |
| 53 |
delete m_dataFile; |
| 54 |
} |
| 55 |
|
| 56 |
QString SimCardContacts::contacts() const |
| 57 |
{ |
| 58 |
QString result; |
| 59 |
Q_FOREACH(const QString &data, m_vcards) { |
| 60 |
result += data; |
| 61 |
} |
| 62 |
return result; |
| 63 |
} |
| 64 |
|
| 65 |
QUrl SimCardContacts::vcardFile() const |
| 66 |
{ |
| 67 |
if (m_dataFile) { |
| 68 |
return QUrl::fromLocalFile(m_dataFile->fileName()); |
| 69 |
} else { |
| 70 |
return QUrl(); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
bool SimCardContacts::hasContacts() const |
| 75 |
{ |
| 76 |
return !m_vcards.isEmpty(); |
| 77 |
} |
| 78 |
|
| 79 |
bool SimCardContacts::busy() const |
| 80 |
{ |
| 81 |
return (m_modemsChangedTimer.isActive() || |
| 82 |
m_importingFlag); |
| 83 |
} |
| 84 |
|
| 85 |
void SimCardContacts::onPhoneBookIsValidChanged(bool isValid) |
| 86 |
{ |
| 87 |
QOfonoPhonebook *pb = qobject_cast<QOfonoPhonebook*>(QObject::sender()); |
| 88 |
Q_ASSERT(pb); |
| 89 |
if (isValid) { |
| 90 |
importPhoneBook(pb); |
| 91 |
} else { |
| 92 |
m_pendingPhoneBooks.remove(pb); |
| 93 |
if (m_pendingPhoneBooks.isEmpty()) { |
| 94 |
importDone(); |
| 95 |
} |
| 96 |
pb->deleteLater(); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
void SimCardContacts::onModemsChanged() |
| 101 |
{ |
| 102 |
qDebug() << "Modems changed"; |
| 103 |
startImport(); |
| 104 |
|
| 105 |
Q_FOREACH(QOfonoModem *modem, m_availableModems) { |
| 106 |
importPhoneBook(modem); |
| 107 |
} |
| 108 |
|
| 109 |
if (m_pendingPhoneBooks.size() == 0) { |
| 110 |
importDone(); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
void SimCardContacts::onManagerChanged() |
| 115 |
{ |
| 116 |
startImport(); |
| 117 |
|
| 118 |
// clear modem list |
| 119 |
Q_FOREACH(QObject *m, m_availableModems) { |
| 120 |
disconnect(m); |
| 121 |
m->deleteLater(); |
| 122 |
} |
| 123 |
m_availableModems.clear(); |
| 124 |
|
| 125 |
if (!m_ofonoManager->available()) { |
| 126 |
qWarning() << "Manager not available;"; |
| 127 |
return; |
| 128 |
} |
| 129 |
|
| 130 |
QStringList modems = m_ofonoManager->modems(); |
| 131 |
Q_FOREACH(const QString &modem, modems) { |
| 132 |
QOfonoModem *m = new QOfonoModem(this); |
| 133 |
|
| 134 |
m->setModemPath(modem); |
| 135 |
m_availableModems << m; |
| 136 |
|
| 137 |
importPhoneBook(m); |
| 138 |
|
| 139 |
connect(m, SIGNAL(interfacesChanged(QStringList)), |
| 140 |
SLOT(reload())); |
| 141 |
connect(m, SIGNAL(validChanged(bool)), |
| 142 |
SLOT(reload())); |
| 143 |
|
| 144 |
} |
| 145 |
|
| 146 |
if (m_pendingPhoneBooks.size() == 0) { |
| 147 |
importDone(); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
bool SimCardContacts::importPhoneBook(QOfonoModem *modem) |
| 152 |
{ |
| 153 |
if (hasPhoneBook(modem)) { |
| 154 |
QOfonoPhonebook *pb = new QOfonoPhonebook(this); |
| 155 |
pb->setModemPath(modem->modemPath()); |
| 156 |
m_pendingPhoneBooks << pb; |
| 157 |
if (pb->isValid()) { |
| 158 |
importPhoneBook(pb); |
| 159 |
} else { |
| 160 |
connect(pb, |
| 161 |
SIGNAL(validChanged(bool)), |
| 162 |
SLOT(onPhoneBookIsValidChanged(bool)), |
| 163 |
Qt::QueuedConnection); |
| 164 |
} |
| 165 |
return true; |
| 166 |
} else { |
| 167 |
qDebug() << "Modem" << modem->modemPath() << "does not have phonebook interface"; |
| 168 |
} |
| 169 |
return false; |
| 170 |
} |
| 171 |
|
| 172 |
void SimCardContacts::importPhoneBook(QOfonoPhonebook *phoneBook) |
| 173 |
{ |
| 174 |
connect(phoneBook, |
| 175 |
SIGNAL(importReady(QString)), |
| 176 |
SLOT(onPhoneBookImported(QString))); |
| 177 |
connect(phoneBook, |
| 178 |
SIGNAL(importFailed()), |
| 179 |
SLOT(onPhoneBookImportFail())); |
| 180 |
|
| 181 |
phoneBook->beginImport(); |
| 182 |
} |
| 183 |
|
| 184 |
void SimCardContacts::onPhoneBookImported(const QString &vcardData) |
| 185 |
{ |
| 186 |
QOfonoPhonebook *pb = qobject_cast<QOfonoPhonebook*>(QObject::sender()); |
| 187 |
Q_ASSERT(pb); |
| 188 |
|
| 189 |
if (!vcardData.trimmed().isEmpty()) { |
| 190 |
m_vcards << vcardData; |
| 191 |
} |
| 192 |
m_pendingPhoneBooks.remove(pb); |
| 193 |
if (m_pendingPhoneBooks.isEmpty()) { |
| 194 |
importDone(); |
| 195 |
} |
| 196 |
pb->deleteLater(); |
| 197 |
} |
| 198 |
|
| 199 |
void SimCardContacts::onPhoneBookImportFail() |
| 200 |
{ |
| 201 |
QOfonoPhonebook *pb = qobject_cast<QOfonoPhonebook*>(QObject::sender()); |
| 202 |
Q_ASSERT(pb); |
| 203 |
qWarning() << "Fail to import contacts from:" << pb->modemPath(); |
| 204 |
|
| 205 |
m_pendingPhoneBooks.remove(pb); |
| 206 |
if (m_pendingPhoneBooks.isEmpty()) { |
| 207 |
importDone(); |
| 208 |
} |
| 209 |
pb->deleteLater(); |
| 210 |
Q_EMIT importFail(); |
| 211 |
} |
| 212 |
|
| 213 |
void SimCardContacts::startImport() |
| 214 |
{ |
| 215 |
m_importingFlag = true; |
| 216 |
Q_EMIT busyChanged(); |
| 217 |
if (!m_importing.tryLock()) { |
| 218 |
qDebug() << "Import in progress."; |
| 219 |
cancel(); |
| 220 |
if (!m_importing.tryLock()) { |
| 221 |
qWarning() << "Fail to cancel current import"; |
| 222 |
return; |
| 223 |
} |
| 224 |
} |
| 225 |
m_vcards.clear(); |
| 226 |
Q_EMIT contactsChanged(); |
| 227 |
} |
| 228 |
|
| 229 |
void SimCardContacts::importDone() |
| 230 |
{ |
| 231 |
writeData(); |
| 232 |
m_importing.unlock(); |
| 233 |
Q_EMIT contactsChanged(); |
| 234 |
m_importingFlag = false; |
| 235 |
Q_EMIT busyChanged(); |
| 236 |
} |
| 237 |
|
| 238 |
void SimCardContacts::writeData() |
| 239 |
{ |
| 240 |
if (m_dataFile) { |
| 241 |
delete m_dataFile; |
| 242 |
m_dataFile = 0; |
| 243 |
} |
| 244 |
if (!m_vcards.isEmpty()) { |
| 245 |
m_dataFile = new QTemporaryFile(); |
| 246 |
m_dataFile->open(); |
| 247 |
Q_FOREACH(const QString &data, m_vcards) { |
| 248 |
m_dataFile->write(data.toUtf8()); |
| 249 |
} |
| 250 |
m_dataFile->close(); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
void SimCardContacts::cancel() |
| 255 |
{ |
| 256 |
Q_FOREACH(QOfonoPhonebook *m, m_pendingPhoneBooks) { |
| 257 |
disconnect(m); |
| 258 |
m->deleteLater(); |
| 259 |
} |
| 260 |
m_pendingPhoneBooks.clear(); |
| 261 |
|
| 262 |
m_importing.unlock(); |
| 263 |
m_vcards.clear(); |
| 264 |
|
| 265 |
m_importingFlag = false; |
| 266 |
Q_EMIT busyChanged(); |
| 267 |
} |
| 268 |
|
| 269 |
bool SimCardContacts::hasPhoneBook(QOfonoModem *modem) |
| 270 |
{ |
| 271 |
return (modem->isValid() && modem->interfaces().contains("org.ofono.Phonebook")); |
| 272 |
} |
| 273 |
|
| 274 |
void SimCardContacts::reload() |
| 275 |
{ |
| 276 |
m_modemsChangedTimer.start(); |
| 277 |
Q_EMIT busyChanged(); |
| 278 |
} |
| 279 |
|
| 280 |
void SimCardContacts::unlockModem(const QString &modemPath) |
| 281 |
{ |
| 282 |
static const QString connService("com.lomiri.connectivity1"); |
| 283 |
static const QString connObject("/com/lomiri/connectivity1/Private"); |
| 284 |
static const QString connInterface("com.lomiri.connectivity1.Private"); |
| 285 |
static const QString connUnlockmodemMethod("UnlockModem"); |
| 286 |
|
| 287 |
QDBusInterface connectivityIface (connService, |
| 288 |
connObject, |
| 289 |
connInterface, |
| 290 |
QDBusConnection::sessionBus(), |
| 291 |
this); |
| 292 |
|
| 293 |
auto reply = connectivityIface.call(connUnlockmodemMethod, modemPath); |
| 294 |
if (reply.type() == QDBusMessage::ErrorMessage) { |
| 295 |
qWarning() << "Failed to unlock modem" << modemPath << reply.errorMessage(); |
| 296 |
} |
| 297 |
} |