summaryrefslogtreecommitdiff
path: root/fbdaemon/fbaggregatorjob.cpp
blob: ad81c7c36660a3562e806d32d27de7abdf23695b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
66
67
68
69
70
71
72
73
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
100
101
102
/*
 * Copyright (C) 2012  Christian Mollekopf <mollekopf@kolabsys.com>
 *
 * This program 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 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "fbaggregatorjob.h"
#include "settings.h"
#include "jobs/probekolabserverjob.h"
#include <jobs/fetchmessagesjob.h>
#include <kdebug.h>
#include <QFile>
#include <QDir>
#include <kolab/kolabobject.h>
#include <kolabdefinitions.h>
#include <freebusy.h>

FBAggregatorJob::FBAggregatorJob(const QString& hostName, qint16 port, const QString& username, QObject* parent)
: KolabJob(hostName, port, username, parent)
{

}

void FBAggregatorJob::startWork(ProbeKolabServerJob* capabilitiesJob)
{
    //TODO freebusy default folder
    if (capabilitiesJob->kolabFolders().values(KOLAB_FOLDER_TYPE_FREEBUSY).isEmpty()) {
        kWarning() << "no freebusy folder found";
        setError(KJob::UserDefinedError);
        emitResult();
        return;
    }
    mFreebusyFolder = capabilitiesJob->kolabFolders().values(KOLAB_FOLDER_TYPE_FREEBUSY).first();
    //TODO create freebusy folder if not existing? (setupkolabfolders job?)
//     mEventFolders = capabilitiesJob->kolabFolders().values(KOLAB_FOLDER_TYPE_EVENT);

    FetchMessagesJob *fetchJob = new FetchMessagesJob(mFreebusyFolder, mSession, this);
    connect( fetchJob, SIGNAL(result(KJob*)), this, SLOT(onFetchFBDone(KJob*)) );
    fetchJob->start();
}


void FBAggregatorJob::onFetchFBDone(KJob *job)
{
    if ( job->error() ) {
        kWarning() << job->errorString();
        setError(KJob::UserDefinedError);
        emitResult();
        return;
    }
    FetchMessagesJob *fetchJob = qobject_cast<FetchMessagesJob*>( job );
    Q_ASSERT(fetchJob);

    std::vector<Kolab::Freebusy> fbList;
    std::string organizerName;
    std::string organizerEmail;
    foreach (const KMime::Message::Ptr &msg, fetchJob->getMessages()) {
        kDebug() << "aggregating fb object: " << msg->subject()->asUnicodeString();
//         kDebug() << msg->encodedContent();
        Kolab::KolabObjectReader reader(msg);
        if (reader.getType() != Kolab::FreebusyObject) {
            kWarning() << "wrong object type in freebusy folder";
            continue;
        }
        fbList.push_back(reader.getFreebusy());
        if (organizerEmail.empty()) {
            organizerEmail = reader.getFreebusy().organizer().email();
            organizerName = reader.getFreebusy().organizer().name();
        }
    }

    Kolab::Freebusy aggregatedFb = Kolab::FreebusyUtils::aggregateFreeBusy(fbList, organizerEmail, organizerName, true);

//     kDebug() << QString::fromStdString(Kolab::FreebusyUtils::toIFB(aggregatedFb));
    if (QDir::setCurrent(Settings::instance().getAggregatedICalOutputDirectory())) {
        QFile file(QString::fromLatin1("%1.sfb").arg(mUserName)/*.arg(KDateTime::currentUtcDateTime().toString(KDateTime::ISODate))*/);
        if (!file.open(QIODevice::WriteOnly|QIODevice::Text)) {
            kWarning() << "failed to open file";
            emitResult();
            return;
        }
        QTextStream out(&file);
        out << QString::fromStdString(Kolab::FreebusyUtils::toIFB(aggregatedFb));
        kDebug() << "wrote " << QDir::current().absoluteFilePath (file.fileName());
    } else {
        kWarning() << "failed to change to aggregated ical output directory: " << Settings::instance().getAggregatedICalOutputDirectory();
    }
    
    emitResult();
}