psa: moved harmattan to /opt
[feedingit] / FeedingIt-Sync / feedingitsync.cpp
1 #include "feedingitsync.h"
2
3 #include <libsyncpluginmgr/PluginCbInterface.h>
4 #include <LogMacros.h>
5 #include <QTimer>
6 #include <QDateTime>
7 #include <QMap>
8 #include <QUrl>
9 #include <meventfeed.h>
10
11 extern "C" EventsExample* createPlugin(const QString& aPluginName,
12                                        const Buteo::SyncProfile& aProfile,
13                                        Buteo::PluginCbInterface *aCbInterface)
14 {
15     QFile file("/home/user/out.txt");
16     file.open(QIODevice::WriteOnly | QIODevice::Text);
17
18     QTextStream out(&file);
19     out << "create plugin \n";
20     file.close();
21     return new EventsExample(aPluginName, aProfile, aCbInterface);
22 }
23
24 extern "C" void destroyPlugin(EventsExample*aClient)
25 {
26     delete aClient;
27 }
28
29 EventsExample::EventsExample(const QString& aPluginName,
30                 const Buteo::SyncProfile& aProfile,
31                 Buteo::PluginCbInterface *aCbInterface) :
32                 ClientPlugin(aPluginName, aProfile, aCbInterface)
33 {
34    FUNCTION_CALL_TRACE;
35 }
36
37 EventsExample::~EventsExample()
38 {
39         FUNCTION_CALL_TRACE;
40 }
41
42 bool EventsExample::init()
43 {
44     FUNCTION_CALL_TRACE;
45     QFile file("/home/user/out.txt");
46     file.open(QIODevice::WriteOnly | QIODevice::Text);
47
48     QTextStream out(&file);
49     out << "init plugin \n";
50     file.close();
51    //The sync profiles can have some specific key/value pairs this info
52    //can be accessed by this method.
53    iProperties = iProfile.allNonStorageKeys();
54
55    //return false - if error
56    //syncfw will call this method first if the plugin is able to initialize properly
57    //and its ready for sync it should return 'true' in case of any error return false.
58    return true;
59 }
60
61 bool EventsExample::uninit()
62 {
63    FUNCTION_CALL_TRACE;
64    // called before unloading the plugin , the plugin should clean up
65    return true;
66 }
67
68 bool EventsExample::startSync()
69 {
70    FUNCTION_CALL_TRACE;
71
72    // This method is called after init(), the plugin is expected to return
73    // either true or false based on if the sync was started successfully or
74    // it failed for some reason
75    //call appropriate slots based on the status of operation success/failed...
76
77    // Create a new GConfItem object
78    GConfItem *boolItem = new GConfItem("/apps/ControlPanel/FeedingIt/EnableUpdates");
79    if (!boolItem->value().isValid()) {
80        boolItem->set(false);
81    }
82
83    // Read value
84    if (boolItem->value().toBool()) {
85         GConfItem *updateFreq = new GConfItem("/apps/ControlPanel/FeedingIt/UpdateFrequency");
86
87         if (!updateFreq->value().isValid()) {
88             updateFreq->set(4);
89         }
90         GConfItem *runsItem = new GConfItem("/apps/ControlPanel/FeedingIt/NumberOfRuns");
91         if (!runsItem->value().isValid()) {
92             runsItem->set(0);
93         }
94         runsItem->set(runsItem->value().toInt()+1);
95         if (runsItem->value().toInt() >= updateFreq->value().toInt()) {
96             QDBusInterface feedingitdbus("org.marcoz.feedingit", "/org/marcoz/feedingit/update", "org.marcoz.feedingit");
97             feedingitdbus.call("UpdateAll");
98             runsItem->set(0);
99         }
100    }
101    QTimer::singleShot(60000, this, SLOT(syncSuccess()));
102
103    return true;
104 }
105
106 void EventsExample::abortSync(Sync::SyncStatus aStatus)
107 {
108    FUNCTION_CALL_TRACE;
109    Q_UNUSED(aStatus);
110    // This method is called if used cancels the
111    // sync in between , with the applet use case
112    // it should not ideally happen as there is no UI
113    // in case of device sync and accounts sync we have
114    // a cancel button
115 }
116
117 bool EventsExample::cleanUp()
118 {
119    FUNCTION_CALL_TRACE;
120
121    // this method is called in case of account being deleted
122    // or the profile being deleted from UI in case of applet
123    // it will not be called ....need to check as if there
124    // can be any use case for this
125    return true;
126 }
127
128 Buteo::SyncResults EventsExample::getSyncResults() const
129 {
130         FUNCTION_CALL_TRACE;
131         return iResults;
132 }
133
134 void EventsExample::connectivityStateChanged(Sync::ConnectivityType aType,
135                 bool aState)
136 {
137    FUNCTION_CALL_TRACE;
138    // This function notifies of the plugin of any connectivity related state changes
139    LOG_DEBUG("Received connectivity change event:" << aType << " changed to " << aState);
140    if ((aType == Sync::CONNECTIVITY_INTERNET) && (aState == false)) {
141        // Network disconnect!!
142    }
143 }
144
145 void EventsExample::syncSuccess()
146 {
147    FUNCTION_CALL_TRACE;
148    updateResults(Buteo::SyncResults(QDateTime::currentDateTime(), Buteo::SyncResults::SYNC_RESULT_SUCCESS, Buteo::SyncResults::NO_ERROR));
149    //Notify Sync FW of result - Now sync fw will call uninit and then will unload plugin
150    emit success(getProfileName(), "Success!!");
151 }
152
153 void EventsExample::syncFailed()
154 {
155    FUNCTION_CALL_TRACE;
156    //Notify Sync FW of result - Now sync fw will call uninit and then will unload plugin
157    updateResults(Buteo::SyncResults(QDateTime::currentDateTime(),
158                  Buteo::SyncResults::SYNC_RESULT_FAILED, Buteo::SyncResults::ABORTED));
159    emit error(getProfileName(), "Error!!", Buteo::SyncResults::SYNC_RESULT_FAILED);
160 }
161
162 void EventsExample::updateResults(const Buteo::SyncResults &aResults)
163 {
164    FUNCTION_CALL_TRACE;
165    iResults = aResults;
166    iResults.setScheduled(true);
167 }
168
169 void EventsExample::updateFeed()
170 {
171    FUNCTION_CALL_TRACE;
172
173    GConfItem *boolItem = new GConfItem("/apps/ControlPanel/FeedingIt/EnableFeed");
174
175    // Read value
176    if (boolItem->value().toBool()) {
177        bool success = false;
178        //Ok assuming that we now have the data that needs to be updated to event feed
179        qlonglong id  = MEventFeed::instance()->addItem(QString("icon-m-transfer-sync"),
180                                QString("Update Started"),
181                                QString("Event Feed updated"),
182                                QStringList(),
183                                QDateTime::currentDateTime(),
184                                QString(),
185                                false,
186                                QUrl(),
187                                QString("FeedingIt"),
188                                QString("FeedingIt RSS Reader"));
189        if (id != -1) {
190                 success = true;
191        }
192        if(success)
193            syncSuccess();
194        else
195             syncFailed();
196    } else {
197        syncSuccess();
198    }
199 }