Fixed bug, now it exits if gcs lib cannot be initialized
[meego-garage:garage-netbook-ui.git] / model.cpp
1 #include "model.h"
2 #include "categorymodel.h"
3 #include "garageclientservices.h"
4
5 #include <QDebug>
6 #include <QtGlobal>
7 #include <QList>
8 #include <QStandardItem>
9 #include <QApplication>
10
11 #include "image.h"
12 #include "messagebox.h"
13
14 using namespace MeeGoGarage;
15
16 // static instance variable to store singleton instance
17 QSharedPointer<Model> Model::mInstance;
18
19 Q_DECLARE_METATYPE(QWeakPointer<Category>);
20
21 Model::Model(QObject *parent) :
22     QObject(parent),
23     mInitialized(false)
24 {
25     garageClientServices();
26 }
27
28 Model::~Model()
29 {
30 }
31
32 void Model::finish()
33 {
34     GarageClientServices * gcs = garageClientServices();
35     if (gcs) {
36         gcs->finish();
37     }
38 }
39
40 QSharedPointer<Model> Model::model()
41 {
42     if (mInstance.isNull()) {
43         QSharedPointer<Model> instance(new Model(0));
44         mInstance = instance;
45     }
46     return mInstance;
47 }
48
49 GarageClientServices * Model::garageClientServices()
50 {
51     if (mGarageClientServices.isNull()) {
52         mGarageClientServices = GarageClientServices::getInstance();
53         connect (mGarageClientServices.data(), SIGNAL(garageClientServicesInitialized(int)),
54                 this, SLOT(onGarageClientServicesInitialized(int)));
55     }
56
57     if (mGarageClientServices.isNull())
58         qCritical() << "GarageClientServices not accessible";
59
60     return mGarageClientServices.data();
61 }
62
63 void Model::onGarageClientServicesInitialized(int error)
64 {
65     //qDebug() << __func__;
66     if (error) {
67         MessageBox::information(
68                 QApplication::activeWindow(),
69                 "Error",
70                 "Error initializing Garage Client Services library",
71                 QDialogButtonBox::Ok, QDialogButtonBox::Ok);
72         QApplication::exit(error);
73         return;
74     }
75
76     Q_CHECK_PTR( garageClientServices()->getCatalog() );
77     mInitialized = true;
78     processPendingRequests();
79 }
80
81 void Model::requestCategoryModelUpdate(CategoryModel *categoryModel)
82 {
83     //qDebug() << __func__;
84
85     mPendingCategoryModelUpdate.append(categoryModel);
86     processPendingRequests();
87 }
88
89 void Model::processPendingRequests()
90 {
91     qDebug() << __func__;
92
93     if (!mInitialized)
94         return;
95
96     Catalog * catalog = garageClientServices()->getCatalog();
97     if (catalog) {
98         QList<Category *> categoryList = catalog->getCategoryList();
99
100         while (!mPendingCategoryModelUpdate.isEmpty()) {
101             CategoryModel * categoryModel = mPendingCategoryModelUpdate.takeFirst();
102             categoryModel->setCategoryList(categoryList);
103         }
104     }
105 }
106
107 QIcon Model::getApplicationIcon(Application * application)
108 {
109     QVariant val = application->property("icon");
110     if (val.isValid()) {
111         QIcon icon = val.value<QIcon>();
112         return icon;
113     }
114
115     const Image * image;
116
117     QIcon icon;
118     image = application->getIconImage(Application::LargeIcon);
119     if (image)
120         icon.addFile(image->getImageLocalPath());
121     image = application->getIconImage(Application::MediumIcon);
122     if (image)
123         icon.addFile(image->getImageLocalPath());
124     image = application->getIconImage(Application::SmallIcon);
125     if (image)
126         icon.addFile(image->getImageLocalPath());
127     val.setValue<QIcon>(icon);
128     application->setProperty("icon", val);
129
130     return icon;
131 }
132
133 QString MeeGoGarage::getDurationString(int seconds)
134 {
135     QTime duration;
136     duration = duration.addSecs(seconds);
137     QString format;
138     if (duration.hour() > 0)
139         format = "H'h'mm'm'ss's'";
140     else if (duration.minute() > 0)
141         format = "m'm'ss's'";
142     else
143         format = "s's'";
144     //qDebug() << duration.toString(format);
145     return duration.toString(format);
146 }