Include QCoreApplication for compiling error.
[meego-garage:garage-client-services.git] / src / garageclientservices.cpp
1 #include "garageclientservices.h"
2 #include <QTimer>
3 #include <QFile>
4 #include <QDir>
5 #include <QNetworkAccessManager>
6 #include <QNetworkProxy>
7 #include "errorhandler.h"
8 #include "applicationmanager.h"
9 #include <QTranslator>
10 #include <QCoreApplication>
11
12 using namespace MeeGoGarage;
13
14 QNetworkProxy *GarageClientServices::systemHttpProxy = 0;
15
16 static GarageClientServices* instance = 0;
17
18 GarageClientServices* GarageClientServices::getInstance()
19 {
20     if (instance == 0) {
21         instance = new GarageClientServices();
22
23         // configure packagekit
24         //QStringList hints;
25         //hints << "locale=en_US.UTF-8";
26         //hints << "background=false";
27         //hints << "interactive=true";
28         //PackageKit::Client::instance()->setHints(hints);
29     }
30
31     return instance;
32 }
33
34 void GarageClientServices::finish()
35 {
36     ApplicationManager * appManager = ApplicationManager::instance();
37     if (appManager) {
38         appManager->saveStatus();
39     }
40 }
41
42 ErrorHandler* GarageClientServices::getErrorHandler()
43 {
44     return errorHandler;
45 }
46
47
48 GarageClientServices::GarageClientServices() :
49     allCatalog(0), networkAccessManager(0), webClient(0), httpProxy(0)
50 {
51     QTranslator appTranslator;
52     appTranslator.load("/usr/share/garage-client-services/data/translations/garage-client-services_"
53                        + QLocale::system().name());
54     if (QCoreApplication::instance()) {
55         QCoreApplication::instance()->installTranslator(&appTranslator);
56     }
57
58     errorHandler = new ErrorHandler(this);
59     connect(errorHandler, SIGNAL(error(GarageClientServices::ErrorCode, QString)),
60             SIGNAL(error(GarageClientServices::ErrorCode, QString)));
61
62     qDebug() << "Init config file ...";
63     if (!GarageSettings::initConfigFile()) {
64         emit error(CriticalError, tr("Init config file failed"));
65         return;
66     }
67
68     qDebug() << "Init config file done.";
69
70     QNetworkProxy proxy = GarageSettings::getClientHttpProxy(QUrl("http://meego.com"));
71     QNetworkProxy::setApplicationProxy(proxy);
72     qDebug() << "The garage client proxy is set to " << proxy.hostName() << ":" << proxy.port();
73
74     networkAccessManager = new QNetworkAccessManager(this);
75
76     int count = GarageSettings::getCatalogCount();
77     catalogCount = 0;
78     int i;
79     for (i = 0; i < count; i++) {
80         Catalog::CatalogSetting catalogSetting = GarageSettings::catalogSetting(i);
81         catalogSetting.dumpCatalogSetting();
82
83         if (!catalogSetting.enabled)
84             continue;
85
86         catalogCount ++;
87         //Set catalog section "Catalog1, Catalog2"
88         Catalog *catalog = new Catalog(catalogSetting);
89         connect(catalog, SIGNAL(catalogInitialized(bool)), SLOT(catalogInitialized(bool)));
90     }
91
92     qDebug() << "GarageClientService construct done. Totally " << catalogCount << " catalogs are enabled.";
93 }
94
95 GarageClientServices::~GarageClientServices()
96 {
97     if (allCatalog) {
98         delete allCatalog;
99         allCatalog = 0;
100     }
101
102     QListIterator<Catalog*> i(catalogList);
103     while(i.hasNext()) {
104         delete i.next();
105     }
106     catalogList.clear();
107
108     if (errorHandler) {
109         delete errorHandler;
110         errorHandler = 0;
111     }
112
113     if (networkAccessManager) {
114         delete networkAccessManager;
115         networkAccessManager = 0;
116     }
117
118     if (webClient) {
119         delete webClient;
120         webClient = 0;
121     }
122
123     if (httpProxy) {
124         delete httpProxy;
125         httpProxy = 0;
126     }
127
128     if (instance == this) {
129         instance = 0;
130     }
131 }
132
133 void GarageClientServices::catalogInitialized(bool initializedSuccess)
134 {
135     Catalog *catalog = qobject_cast<Catalog*>(sender());
136
137     if (!initializedSuccess) {
138         qDebug() << "Catalog " << catalog->id() << " initialized failed.";
139         catalogCount --;
140     } else {
141         qDebug() << "Catalog " << catalog->id() << " initialized success.";
142         catalogList.append(catalog);
143
144         if (allCatalog == NULL)
145             allCatalog = new Catalog();
146
147         allCatalog->add(catalog);
148     }
149
150     if (catalogList.count() == catalogCount) {
151         if (catalogCount == 0) {
152             emit error(CriticalError, tr("No catalog is ready for the garage!"));
153             emit garageClientServicesInitialized(-1);
154             return;
155         }
156
157         allCatalog->categoryListIsReady = true;
158         allCatalog->applicationListIsReady = true;
159
160         //First catalog ready
161         initGarageService();
162     }
163
164     return;
165 }
166
167 Catalog *GarageClientServices::getCatalog()
168 {
169     return allCatalog;
170 }
171
172 QList<Catalog*> GarageClientServices::getCatalogList()
173 {
174     return catalogList;
175 }
176
177 void GarageClientServices::initGarageService()
178 {
179     //Initialization done, we are able to create catalog
180     emit garageClientServicesInitialized(0);
181 }
182
183 QUrl GarageClientServices::getProjectUrl()
184 {
185     return GarageSettings::getProjectUrl();
186 }