MinimalHistoryBackend.mm 9.19 KB
Newer Older
Alexandre Lision's avatar
Alexandre Lision committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/************************************************************************************
 *   Copyright (C) 2014-2015 by Savoir-Faire Linux                                       *
 *   Author : Emmanuel Lepage Vallee <emmanuel.lepage@savoirfairelinux.com>         *
 *                                                                                  *
 *   This library is free software; you can redistribute it and/or                  *
 *   modify it under the terms of the GNU Lesser General Public                     *
 *   License as published by the Free Software Foundation; either                   *
 *   version 2.1 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              *
 *   Lesser General Public License for more details.                                *
 *                                                                                  *
 *   You should have received a copy of the GNU Lesser General Public               *
 *   License along with this library; if not, write to the Free Software            *
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA *
 ***********************************************************************************/
#include "minimalhistorybackend.h"

//Qt
#include <QtCore/QFile>
23
#include <QtCore/QDir>
Alexandre Lision's avatar
Alexandre Lision committed
24
#include <QtCore/QHash>
25 26
#include <QtWidgets/QApplication>
#include <QtCore/QStandardPaths>
Alexandre Lision's avatar
Alexandre Lision committed
27 28

//Ring
29 30 31 32 33
#include <call.h>
#include <account.h>
#include <person.h>
#include <contactmethod.h>
#include <historymodel.h>
Alexandre Lision's avatar
Alexandre Lision committed
34

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
class MinimalHistoryEditor : public CollectionEditor<Call>
{
public:
    MinimalHistoryEditor(CollectionMediator<Call>* m, MinimalHistoryBackend* parent);
    virtual bool save       ( const Call* item ) override;
    virtual bool remove     ( const Call* item ) override;
    virtual bool edit       ( Call*       item ) override;
    virtual bool addNew     ( const Call* item ) override;
    virtual bool addExisting( const Call* item ) override;

private:
    virtual QVector<Call*> items() const override;

    //Helpers
    void saveCall(QTextStream& stream, const Call* call);
    bool regenFile(const Call* toIgnore);

    //Attributes
    QVector<Call*> m_lItems;
    MinimalHistoryBackend* m_pCollection;
};

MinimalHistoryEditor::MinimalHistoryEditor(CollectionMediator<Call>* m, MinimalHistoryBackend* parent) :
CollectionEditor<Call>(m),m_pCollection(parent)
{

}

MinimalHistoryBackend::MinimalHistoryBackend(CollectionMediator<Call>* mediator) :
CollectionInterface(new MinimalHistoryEditor(mediator,this)),m_pMediator(mediator)
{
Alexandre Lision's avatar
Alexandre Lision committed
66

67
}
Alexandre Lision's avatar
Alexandre Lision committed
68 69 70 71 72 73

MinimalHistoryBackend::~MinimalHistoryBackend()
{

}

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
void MinimalHistoryEditor::saveCall(QTextStream& stream, const Call* call)
{
    const QString direction = (call->direction()==Call::Direction::INCOMING)?
    Call::HistoryStateName::INCOMING : Call::HistoryStateName::OUTGOING;

    const Account* a = call->account();
    stream << QString("%1=%2\n").arg(Call::HistoryMapFields::CALLID          ).arg(call->historyId()                     );
    stream << QString("%1=%2\n").arg(Call::HistoryMapFields::TIMESTAMP_START ).arg(call->startTimeStamp()         );
    stream << QString("%1=%2\n").arg(Call::HistoryMapFields::TIMESTAMP_STOP  ).arg(call->stopTimeStamp()          );
    stream << QString("%1=%2\n").arg(Call::HistoryMapFields::ACCOUNT_ID      ).arg(a?QString(a->id()):""          );
    stream << QString("%1=%2\n").arg(Call::HistoryMapFields::DISPLAY_NAME    ).arg(call->peerName()               );
    stream << QString("%1=%2\n").arg(Call::HistoryMapFields::PEER_NUMBER     ).arg(call->peerContactMethod()->uri() );
    stream << QString("%1=%2\n").arg(Call::HistoryMapFields::DIRECTION       ).arg(direction                      );
    stream << QString("%1=%2\n").arg(Call::HistoryMapFields::MISSED          ).arg(call->isMissed()               );
    stream << QString("%1=%2\n").arg(Call::HistoryMapFields::RECORDING_PATH  ).arg(call->recordingPath()          );
    stream << QString("%1=%2\n").arg(Call::HistoryMapFields::CONTACT_USED    ).arg(false                          );//TODO
    if (call->peerContactMethod()->contact()) {
        stream << QString("%1=%2\n").arg(Call::HistoryMapFields::CONTACT_UID  ).arg(
                                                                                    QString(call->peerContactMethod()->contact()->uid())
                                                                                    );
    }
    stream << "\n";
    stream.flush();
}

bool MinimalHistoryEditor::regenFile(const Call* toIgnore)
Alexandre Lision's avatar
Alexandre Lision committed
100
{
101 102 103 104 105 106 107 108 109 110 111 112 113 114
    QDir dir(QString('/'));
    dir.mkpath(QStandardPaths::writableLocation(QStandardPaths::DataLocation) + QLatin1Char('/') + QString());

    QFile file(QStandardPaths::writableLocation(QStandardPaths::DataLocation) + QLatin1Char('/') +"history.ini");
    if ( file.open(QIODevice::WriteOnly | QIODevice::Text) ) {
        QTextStream stream(&file);
        for (const Call* c : HistoryModel::instance()->getHistoryCalls()) {
            if (c != toIgnore)
                saveCall(stream, c);
        }
        file.close();
        return true;
    }
    return false;
Alexandre Lision's avatar
Alexandre Lision committed
115 116
}

117
bool MinimalHistoryEditor::save(const Call* call)
Alexandre Lision's avatar
Alexandre Lision committed
118
{
119 120 121 122
    if (call->collection()->editor<Call>() != this)
        return addNew(call);

    return regenFile(nullptr);
Alexandre Lision's avatar
Alexandre Lision committed
123 124
}

125
bool MinimalHistoryEditor::remove(const Call* item)
Alexandre Lision's avatar
Alexandre Lision committed
126
{
127
    return regenFile(item);
Alexandre Lision's avatar
Alexandre Lision committed
128 129 130 131
}

bool MinimalHistoryEditor::edit( Call* item)
{
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
    Q_UNUSED(item)
    return false;
}

bool MinimalHistoryEditor::addNew(const Call* call)
{
    QDir dir(QString('/'));
    dir.mkpath(QStandardPaths::writableLocation(QStandardPaths::DataLocation) + QLatin1Char('/') + QString());

    if ((call->collection() && call->collection()->editor<Call>() == this)  || call->historyId().isEmpty()) return false;
    //TODO support \r and \n\r end of line
    QFile file(QStandardPaths::writableLocation(QStandardPaths::DataLocation) + QLatin1Char('/')+"history.ini");

    if ( file.open(QIODevice::Append | QIODevice::Text) ) {
        QTextStream streamFileOut(&file);
        saveCall(streamFileOut, call);
        file.close();

        const_cast<Call*>(call)->setCollection(m_pCollection);
        addExisting(call);
        return true;
    }
    else
        qWarning() << "Unable to save history";
    return false;
Alexandre Lision's avatar
Alexandre Lision committed
157 158
}

159
bool MinimalHistoryEditor::addExisting(const Call* item)
Alexandre Lision's avatar
Alexandre Lision committed
160
{
161 162 163
    m_lItems << const_cast<Call*>(item);
    mediator()->addItem(item);
    return true;
Alexandre Lision's avatar
Alexandre Lision committed
164 165 166 167
}

QVector<Call*> MinimalHistoryEditor::items() const
{
168
    return m_lItems;
Alexandre Lision's avatar
Alexandre Lision committed
169 170 171 172
}

QString MinimalHistoryBackend::name () const
{
173
    return QObject::tr("Minimal history backend");
Alexandre Lision's avatar
Alexandre Lision committed
174 175 176 177
}

QString MinimalHistoryBackend::category () const
{
178
    return QObject::tr("History");
Alexandre Lision's avatar
Alexandre Lision committed
179 180 181 182
}

QVariant MinimalHistoryBackend::icon() const
{
183
    return QVariant();
Alexandre Lision's avatar
Alexandre Lision committed
184 185 186 187
}

bool MinimalHistoryBackend::isEnabled() const
{
188
    return true;
Alexandre Lision's avatar
Alexandre Lision committed
189 190 191 192
}

bool MinimalHistoryBackend::load()
{
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
    QFile file(QStandardPaths::writableLocation(QStandardPaths::DataLocation) + QLatin1Char('/') +"history.ini");
    if ( file.open(QIODevice::ReadOnly | QIODevice::Text) ) {
        QMap<QString,QString> hc;
        while (!file.atEnd()) {
            QByteArray line = file.readLine().trimmed();

            //The item is complete
            if ((line.isEmpty() || !line.size()) && hc.size()) {
                Call* pastCall = Call::buildHistoryCall(hc);
                if (pastCall->peerName().isEmpty()) {
                    pastCall->setPeerName(QObject::tr("Unknown"));
                }
                pastCall->setRecordingPath(hc[ Call::HistoryMapFields::RECORDING_PATH ]);
                pastCall->setCollection(this);

                editor<Call>()->addExisting(pastCall);
                hc.clear();
Alexandre Lision's avatar
Alexandre Lision committed
210
            }
211 212 213 214 215 216 217 218 219 220 221 222
            // Add to the current set
            else {
                const int idx = line.indexOf("=");
                if (idx >= 0)
                    hc[line.left(idx)] = line.right(line.size()-idx-1);
            }
        }
        return true;
    }
    else
        qWarning() << "History doesn't exist or is not readable";
    return false;
Alexandre Lision's avatar
Alexandre Lision committed
223 224 225 226
}

bool MinimalHistoryBackend::reload()
{
227 228
    return false;
}
Alexandre Lision's avatar
Alexandre Lision committed
229 230 231

CollectionInterface::SupportedFeatures MinimalHistoryBackend::supportedFeatures() const
{
232 233 234 235 236 237 238
    return (CollectionInterface::SupportedFeatures) (
                                                     CollectionInterface::SupportedFeatures::NONE  |
                                                     CollectionInterface::SupportedFeatures::LOAD  |
                                                     CollectionInterface::SupportedFeatures::CLEAR |
                                                     CollectionInterface::SupportedFeatures::REMOVE|
                                                     CollectionInterface::SupportedFeatures::ADD   );
}
Alexandre Lision's avatar
Alexandre Lision committed
239 240 241

bool MinimalHistoryBackend::clear()
{
242 243 244
    /* TODO: insert confirm dialog? */
    QFile::remove(QStandardPaths::writableLocation(QStandardPaths::DataLocation) + QLatin1Char('/') + "history.ini");
    return true;
Alexandre Lision's avatar
Alexandre Lision committed
245 246 247 248
}

QByteArray MinimalHistoryBackend::id() const
{
249
    return "mhb";
Alexandre Lision's avatar
Alexandre Lision committed
250
}