summaryrefslogtreecommitdiff
path: root/fbdaemon/fbaggregatorjob.cpp
blob: a422da6a919f7524bfbaecbbd56a8a521460b966 (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
103
104
105
/*
 * 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 Affero 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero 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 <kolab/kolabdefinitions.h>
#include <kolab/freebusy.h>
#include <kolab/errorhandler.h>
#include <kolab/commonconversion.h>

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

}

void FBAggregatorJob::startWork(ProbeKolabServerJob* capabilitiesJob)
{
    Debug() << "starting aggregator job for " << mUserName;
    if (capabilitiesJob->kolabFolders().values(KOLAB_FOLDER_TYPE_FREEBUSY).isEmpty()) {
        kWarning() << "no freebusy folder found";
        setError(KJob::UserDefinedError);
        logout();
        return;
    }
    mFreebusyFolder = capabilitiesJob->kolabFolders().values(KOLAB_FOLDER_TYPE_FREEBUSY).first();

    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() ) {
        Warning() << job->errorString();
        setError(KJob::UserDefinedError);
        logout();
        return;
    }
    FetchMessagesJob *fetchJob = qobject_cast<FetchMessagesJob*>( job );
    Q_ASSERT(fetchJob);

    //Filter list and extract organizer from existing objects
    std::vector<Kolab::Freebusy> fbList;
    foreach (const KMime::Message::Ptr &msg, fetchJob->getMessages()) {
        Debug() << "aggregating fb object: " << msg->subject()->asUnicodeString();
//         kDebug() << msg->encodedContent();
        Kolab::KolabObjectReader reader(msg);
        if (reader.getType() != Kolab::FreebusyObject) {
            Warning() << "wrong object type in freebusy folder";
            continue;
        }
        fbList.push_back(reader.getFreebusy());
    }

    if (fbList.empty()) {
        Debug() << "No f/b objects to aggregate";
        logout();
        return;
    }
    std::string organizerName;
    std::string organizerEmail(Kolab::Conversion::toStdString(mUserName));
    const Kolab::Freebusy aggregatedFb = Kolab::FreebusyUtils::aggregateFreeBusy(fbList, organizerEmail, organizerName, true);

//     kDebug() << QString::fromStdString(Kolab::FreebusyUtils::toIFB(aggregatedFb));
    
    QFile file(QDir(Settings::instance().getAggregatedICalOutputDirectory()).absoluteFilePath(QString::fromLatin1("%1.ifb").arg(mUserName)/*.arg(KDateTime::currentUtcDateTime().toString(KDateTime::ISODate))*/));
    if (!file.open(QIODevice::WriteOnly|QIODevice::Text)) {
        Warning() << "failed to create aggregated f/b file";
        logout();
        return;
    }
    mGeneratedFile = QDir::current().absoluteFilePath(file.fileName());
    QTextStream out(&file);
    out << QString::fromStdString(Kolab::FreebusyUtils::toIFB(aggregatedFb));
    Debug() << "wrote " << mGeneratedFile;
    logout();
}

QString FBAggregatorJob::generatedFile() const
{
    return mGeneratedFile;
}