Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame^] | 1 | /************************************************************************************ |
| 2 | * Copyright (C) 2014-2015 by Savoir-Faire Linux * |
| 3 | * Author : Emmanuel Lepage Vallee <emmanuel.lepage@savoirfairelinux.com> * |
| 4 | * * |
| 5 | * This library is free software; you can redistribute it and/or * |
| 6 | * modify it under the terms of the GNU Lesser General Public * |
| 7 | * License as published by the Free Software Foundation; either * |
| 8 | * version 2.1 of the License, or (at your option) any later version. * |
| 9 | * * |
| 10 | * This library 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 GNU * |
| 13 | * Lesser General Public License for more details. * |
| 14 | * * |
| 15 | * You should have received a copy of the GNU Lesser General Public * |
| 16 | * License along with this library; if not, write to the Free Software * |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * |
| 18 | ***********************************************************************************/ |
| 19 | #include "minimalhistorybackend.h" |
| 20 | |
| 21 | //Qt |
| 22 | #include <QtCore/QFile> |
| 23 | #include <QtCore/QHash> |
| 24 | #include <QtCore/qcoreapplication.h> |
| 25 | |
| 26 | //Ring |
| 27 | #include "call.h" |
| 28 | #include "account.h" |
| 29 | #include "phonenumber.h" |
| 30 | #include "historymodel.h" |
| 31 | |
| 32 | |
| 33 | |
| 34 | MinimalHistoryBackend::~MinimalHistoryBackend() |
| 35 | { |
| 36 | |
| 37 | } |
| 38 | |
| 39 | bool MinimalHistoryEditor::save(const Call* item) |
| 40 | { |
| 41 | Q_UNUSED(item) |
| 42 | return false; |
| 43 | } |
| 44 | |
| 45 | bool MinimalHistoryEditor::append(const Call* item) |
| 46 | { |
| 47 | Q_UNUSED(item) |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | bool MinimalHistoryEditor::remove(Call* item) |
| 52 | { |
| 53 | Q_UNUSED(item) |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | bool MinimalHistoryEditor::edit( Call* item) |
| 58 | { |
| 59 | Q_UNUSED(item) |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | bool MinimalHistoryEditor::addNew( Call* item) |
| 64 | { |
| 65 | Q_UNUSED(item) |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | QVector<Call*> MinimalHistoryEditor::items() const |
| 70 | { |
| 71 | return QVector<Call*>(); |
| 72 | } |
| 73 | |
| 74 | QString MinimalHistoryBackend::name () const |
| 75 | { |
| 76 | return QObject::tr("Minimal history backend"); |
| 77 | } |
| 78 | |
| 79 | QString MinimalHistoryBackend::category () const |
| 80 | { |
| 81 | return QObject::tr("History"); |
| 82 | } |
| 83 | |
| 84 | QVariant MinimalHistoryBackend::icon() const |
| 85 | { |
| 86 | return QVariant(); |
| 87 | } |
| 88 | |
| 89 | bool MinimalHistoryBackend::isEnabled() const |
| 90 | { |
| 91 | return true; |
| 92 | } |
| 93 | |
| 94 | bool MinimalHistoryBackend::load() |
| 95 | { |
| 96 | QFile file(QCoreApplication::applicationDirPath() +"/history.ini"); |
| 97 | if ( file.open(QIODevice::ReadOnly | QIODevice::Text) ) { |
| 98 | QMap<QString,QString> hc; |
| 99 | while (!file.atEnd()) { |
| 100 | QByteArray line = file.readLine().trimmed(); |
| 101 | |
| 102 | //The item is complete |
| 103 | if ((line.isEmpty() || !line.size()) && hc.size()) { |
| 104 | Call* pastCall = Call::buildHistoryCall(hc); |
| 105 | if (pastCall->peerName().isEmpty()) { |
| 106 | pastCall->setPeerName(QObject::tr("Unknown")); |
| 107 | } |
| 108 | pastCall->setRecordingPath(hc[ Call::HistoryMapFields::RECORDING_PATH ]); |
| 109 | pastCall->setBackend(this); |
| 110 | m_pMediator->addItem(pastCall); |
| 111 | hc.clear(); |
| 112 | } |
| 113 | // Add to the current set |
| 114 | else { |
| 115 | const int idx = line.indexOf("="); |
| 116 | if (idx >= 0) |
| 117 | hc[line.left(idx)] = line.right(line.size()-idx-1); |
| 118 | } |
| 119 | } |
| 120 | return true; |
| 121 | } |
| 122 | else |
| 123 | qWarning() << "History doesn't exist or is not readable"; |
| 124 | return false; |
| 125 | } |
| 126 | |
| 127 | bool MinimalHistoryBackend::reload() |
| 128 | { |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | |
| 133 | // bool MinimalHistoryBackend::append(const Call* call) |
| 134 | // { |
| 135 | // if (call->backend() == this || call->id().isEmpty()) return false; |
| 136 | // //TODO support \r and \n\r end of line |
| 137 | // QFile file(KStandardDirs::locateLocal("appdata","")+"history.ini"); |
| 138 | // if ( file.open(QIODevice::Append | QIODevice::Text) ) { |
| 139 | // const QString direction = (call->direction()==Call::Direction::INCOMING)? |
| 140 | // Call::HistoryStateName::INCOMING : Call::HistoryStateName::OUTGOING; |
| 141 | // QTextStream streamFileOut(&file); |
| 142 | // const Account* a = call->account(); |
| 143 | // streamFileOut << QString("%1=%2\n").arg(Call::HistoryMapFields::CALLID ).arg(call->id() ); |
| 144 | // streamFileOut << QString("%1=%2\n").arg(Call::HistoryMapFields::TIMESTAMP_START ).arg(call->startTimeStamp() ); |
| 145 | // streamFileOut << QString("%1=%2\n").arg(Call::HistoryMapFields::TIMESTAMP_STOP ).arg(call->stopTimeStamp() ); |
| 146 | // streamFileOut << QString("%1=%2\n").arg(Call::HistoryMapFields::ACCOUNT_ID ).arg(a?QString(a->id()):"" ); |
| 147 | // streamFileOut << QString("%1=%2\n").arg(Call::HistoryMapFields::DISPLAY_NAME ).arg(call->peerName() ); |
| 148 | // streamFileOut << QString("%1=%2\n").arg(Call::HistoryMapFields::PEER_NUMBER ).arg(call->peerPhoneNumber()->uri() ); |
| 149 | // streamFileOut << QString("%1=%2\n").arg(Call::HistoryMapFields::DIRECTION ).arg(direction ); |
| 150 | // streamFileOut << QString("%1=%2\n").arg(Call::HistoryMapFields::MISSED ).arg(call->isMissed() ); |
| 151 | // streamFileOut << QString("%1=%2\n").arg(Call::HistoryMapFields::RECORDING_PATH ).arg(call->recordingPath() ); |
| 152 | // streamFileOut << QString("%1=%2\n").arg(Call::HistoryMapFields::CONTACT_USED ).arg(false );//TODO |
| 153 | // if (call->peerPhoneNumber()->contact()) { |
| 154 | // streamFileOut << QString("%1=%2\n").arg(Call::HistoryMapFields::CONTACT_UID ).arg( |
| 155 | // QString(call->peerPhoneNumber()->contact()->uid()) |
| 156 | // ); |
| 157 | // } |
| 158 | // streamFileOut << "\n"; |
| 159 | // streamFileOut.flush(); |
| 160 | // file.close(); |
| 161 | // return true; |
| 162 | // } |
| 163 | // else |
| 164 | // qWarning() << "Unable to save history"; |
| 165 | // return false; |
| 166 | // } |
| 167 | |
| 168 | /** Rewrite the file from scratch |
| 169 | * @todo Eventually check if it is necessary, it will be faster |
| 170 | */ |
| 171 | // bool MinimalHistoryBackend::save(const Call* call) |
| 172 | // { |
| 173 | // Q_UNUSED(call) |
| 174 | // if (call->backend() != this) |
| 175 | // append(call); |
| 176 | // |
| 177 | // //TODO, need to regenerate everything |
| 178 | // /*QFile file(KStandardDirs::locateLocal("appdata","")+"history.ini"); |
| 179 | // if ( file.open(QIODevice::WriteOnly | QIODevice::Text) ) { |
| 180 | // foreach(const Call* call, HistoryModel::instance()->getHistoryCalls()) { |
| 181 | // qDebug() << "HERE" << call->id(); |
| 182 | // } |
| 183 | // return true; |
| 184 | // }*/ |
| 185 | // return false; |
| 186 | // } |
| 187 | |
| 188 | CollectionInterface::SupportedFeatures MinimalHistoryBackend::supportedFeatures() const |
| 189 | { |
| 190 | return (CollectionInterface::SupportedFeatures) ( |
| 191 | CollectionInterface::SupportedFeatures::NONE | |
| 192 | CollectionInterface::SupportedFeatures::LOAD | |
| 193 | CollectionInterface::SupportedFeatures::CLEAR | |
| 194 | // CollectionInterface::SupportedFeatures::REMOVE| |
| 195 | CollectionInterface::SupportedFeatures::ADD ); |
| 196 | } |
| 197 | |
| 198 | ///Edit 'item', the implementation may be a GUI or somehting else |
| 199 | // bool MinimalHistoryBackend::edit( Call* call) |
| 200 | // { |
| 201 | // Q_UNUSED(call) |
| 202 | // return false; |
| 203 | // } |
| 204 | |
| 205 | |
| 206 | // bool MinimalHistoryBackend::remove ( Call* c ) |
| 207 | // { |
| 208 | // Q_UNUSED(c) |
| 209 | // qDebug() << "Removing item is not yet supported"; |
| 210 | // return true; |
| 211 | // } |
| 212 | |
| 213 | ///Add a new item to the backend |
| 214 | // bool MinimalHistoryBackend::addNew( Call* call) |
| 215 | // { |
| 216 | // Q_UNUSED(call) |
| 217 | // return true; |
| 218 | // } |
| 219 | |
| 220 | ///Add a new phone number to an existing item |
| 221 | // bool MinimalHistoryBackend::addPhoneNumber( Call* call , PhoneNumber* number ) |
| 222 | // { |
| 223 | // Q_UNUSED(call) |
| 224 | // Q_UNUSED(number) |
| 225 | // return false; |
| 226 | // } |
| 227 | |
| 228 | bool MinimalHistoryBackend::clear() |
| 229 | { |
| 230 | QFile::remove(QCoreApplication::applicationDirPath()+"/history.ini"); |
| 231 | } |
| 232 | |
| 233 | QByteArray MinimalHistoryBackend::id() const |
| 234 | { |
| 235 | return "mhb"; |
| 236 | } |
| 237 | |
| 238 | // QList<Call*> MinimalHistoryBackend::items() const |
| 239 | // { |
| 240 | // return QList<Call*>(); //TODO |
| 241 | // } |
| 242 | |