Edric Milaret | 67007d1 | 2015-05-07 09:40:09 -0400 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * Copyright (C) 2015 by Savoir-Faire Linux * |
| 3 | * Author: Edric Ladent Milaret <edric.ladent-milaret@savoirfairelinux.com>* |
| 4 | * * |
| 5 | * This program is free software; you can redistribute it and/or modify * |
| 6 | * it under the terms of the GNU General Public License as published by * |
| 7 | * the Free Software Foundation; either version 3 of the License, or * |
| 8 | * (at your option) any later version. * |
| 9 | * * |
| 10 | * This program is distributed in the hope that it will be useful, * |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 13 | * GNU General Public License for more details. * |
| 14 | * * |
| 15 | * You should have received a copy of the GNU General Public License * |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. * |
| 17 | **************************************************************************/ |
| 18 | |
| 19 | #include "windowscontactbackend.h" |
| 20 | |
| 21 | WindowsContactEditor::WindowsContactEditor(CollectionMediator<Person> *m |
| 22 | , WindowsContactBackend *parent) |
| 23 | : CollectionEditor<Person>(m),collection_(parent) |
| 24 | { |
| 25 | |
| 26 | } |
| 27 | |
| 28 | WindowsContactEditor::~WindowsContactEditor() |
| 29 | { |
| 30 | |
| 31 | } |
| 32 | |
| 33 | bool |
| 34 | WindowsContactEditor::save(const Person *item) |
| 35 | { |
| 36 | Q_UNUSED(item) |
| 37 | return false; |
| 38 | } |
| 39 | |
| 40 | bool |
| 41 | WindowsContactEditor::remove(const Person *item) |
| 42 | { |
| 43 | Q_UNUSED(item) |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | bool |
| 48 | WindowsContactEditor::edit(Person *item) |
| 49 | { |
| 50 | Q_UNUSED(item) |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | bool |
| 55 | WindowsContactEditor::addNew(const Person *item) |
| 56 | { |
| 57 | Q_UNUSED(item) |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | bool |
| 62 | WindowsContactEditor::addExisting(const Person *item) |
| 63 | { |
| 64 | items_ << const_cast<Person*>(item); |
| 65 | mediator()->addItem(item); |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | QVector<Person*> |
| 70 | WindowsContactEditor::items() const |
| 71 | { |
| 72 | return items_; |
| 73 | } |
| 74 | |
| 75 | WindowsContactBackend::WindowsContactBackend(CollectionMediator<Person>* mediator, |
| 76 | CollectionInterface* parent) |
| 77 | : CollectionInterface(new WindowsContactEditor(mediator,this), parent) |
| 78 | , mediator_(mediator) |
| 79 | { |
| 80 | |
| 81 | } |
| 82 | |
| 83 | WindowsContactBackend::~WindowsContactBackend() |
| 84 | { |
| 85 | |
| 86 | } |
| 87 | |
| 88 | bool |
| 89 | WindowsContactBackend::load() |
| 90 | { |
| 91 | QtConcurrent::run(this, &WindowsContactBackend::loadRun); |
| 92 | return true; |
| 93 | } |
| 94 | |
| 95 | bool |
| 96 | WindowsContactBackend::loadRun() |
| 97 | { |
| 98 | QDir contactDir(QStandardPaths::writableLocation |
| 99 | (QStandardPaths::HomeLocation) + "/Contacts"); |
| 100 | QStringList filesList = contactDir.entryList(QStringList("*.contact")); |
| 101 | |
| 102 | for(auto contactFileName : filesList) { |
| 103 | QString contactFilePath |
| 104 | (contactDir.absolutePath() + "/" + contactFileName); |
| 105 | QFile contactFile(contactFilePath); |
| 106 | if (contactFile.open(QIODevice::ReadOnly)) { |
| 107 | QXmlStreamReader reader; |
| 108 | Person *p = new Person(); |
| 109 | QVector<ContactMethod*> contactMethod; |
| 110 | reader.setDevice(&contactFile); |
| 111 | while (!reader.atEnd()) { |
| 112 | reader.readNext(); |
| 113 | if (reader.isStartElement()) { |
| 114 | QString name = reader.name().toString(); |
| 115 | if (name == "FormattedName") |
| 116 | p->setFormattedName(reader.readElementText()); |
| 117 | else if (name == "NickName") |
| 118 | p->setNickName(reader.readElementText()); |
| 119 | else if (name == "GivenName") |
| 120 | p->setFirstName(reader.readElementText()); |
| 121 | else if (name == "FamilyName") |
| 122 | p->setFamilyName(reader.readElementText()); |
| 123 | else if (name == "Company") |
| 124 | p->setOrganization(reader.readElementText()); |
| 125 | else if (name == "Department") |
| 126 | p->setDepartment(reader.readElementText()); |
| 127 | else if (name == "Number") { |
| 128 | QString number = reader.readElementText(); |
| 129 | if (not number.isEmpty()) { |
| 130 | ContactMethod *contact = PhoneDirectoryModel::instance()->getNumber(number); |
| 131 | contactMethod.append(contact); |
| 132 | } |
| 133 | } |
| 134 | else if (name == "Photo") { |
| 135 | //FIXME: It seems to be possible to have multiple photo... |
| 136 | reader.readNext(); |
Edric Milaret | db76aa8 | 2015-05-11 16:01:00 -0400 | [diff] [blame^] | 137 | if (reader.name().toString() == "Url") { |
Edric Milaret | 67007d1 | 2015-05-07 09:40:09 -0400 | [diff] [blame] | 138 | QString photoValue = reader.readElementText(); |
Edric Milaret | db76aa8 | 2015-05-11 16:01:00 -0400 | [diff] [blame^] | 139 | QImage photo; |
| 140 | photo.load(photoValue); |
| 141 | p->setPhoto(photo.scaled(sizePhoto_,sizePhoto_, Qt::KeepAspectRatio, |
| 142 | Qt::SmoothTransformation)); |
Edric Milaret | 67007d1 | 2015-05-07 09:40:09 -0400 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | else if (name == "EmailAddress") { |
| 146 | QString address; |
| 147 | bool isPreferred = false; |
| 148 | reader.readNext(); |
| 149 | while (reader.name().toString() != "EmailAddress" |
| 150 | && !reader.atEnd()) { |
| 151 | if (reader.isStartElement()) { |
| 152 | QString tag = reader.name().toString(); |
| 153 | if (tag == "Address") |
| 154 | address = reader.readElementText(); |
| 155 | else if (tag == "Label") |
| 156 | if (reader.readElementText() == "Preferred") |
| 157 | isPreferred = true; |
| 158 | } |
| 159 | reader.readNext(); |
| 160 | } |
| 161 | if (isPreferred) |
| 162 | p->setPreferredEmail(address); |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | if (reader.hasError()) { |
| 167 | qDebug() << reader.errorString(); |
| 168 | } else { |
| 169 | if (contactMethod.size() > 0) |
| 170 | p->setContactMethods(contactMethod); |
| 171 | editor<Person>()->addExisting(p); |
| 172 | } |
| 173 | } else { |
| 174 | qDebug() << "Error Opening contact file : " << contactFileName; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | return false; |
| 179 | } |
| 180 | |
| 181 | bool |
| 182 | WindowsContactBackend::reload() |
| 183 | { |
| 184 | return false; |
| 185 | } |
| 186 | |
| 187 | bool |
| 188 | WindowsContactBackend::clear() |
| 189 | { |
| 190 | return false; |
| 191 | } |
| 192 | |
| 193 | QString |
| 194 | WindowsContactBackend::name() const |
| 195 | { |
| 196 | return "Windows Contact Backend"; |
| 197 | } |
| 198 | |
| 199 | QString |
| 200 | WindowsContactBackend::category() const |
| 201 | { |
| 202 | return "Contacts"; |
| 203 | } |
| 204 | |
| 205 | bool |
| 206 | WindowsContactBackend::isEnabled() const |
| 207 | { |
| 208 | return true; |
| 209 | } |
| 210 | |
| 211 | QByteArray |
| 212 | WindowsContactBackend::id() const |
| 213 | { |
| 214 | return "wincb"; |
| 215 | } |
| 216 | |
| 217 | FlagPack<CollectionInterface::SupportedFeatures> WindowsContactBackend::supportedFeatures() const |
| 218 | { |
| 219 | return ( |
| 220 | CollectionInterface::SupportedFeatures::NONE | |
| 221 | CollectionInterface::SupportedFeatures::LOAD); |
| 222 | } |
| 223 | |