Use of gcsDebug instead of qDebug
[meego-garage:garage-client-services.git] / src / category.cpp
1 #include <QTimer>
2 #include <QStringList>
3 #include <QtDebug>
4 #include "category.h"
5 #include "utils.h"
6 #include "catalog.h"
7 #include "jobtypes.h"
8
9 using namespace MeeGoGarage;
10
11 Category::Category(Catalog* catalog, QObject *parent) :
12     QObject(parent),
13     m_catalog(catalog),
14     m_applicationListRequested(false),
15     m_internalRequest(0),
16     m_totalApplicationCount(0)
17 {
18 }
19
20 QString Category::id() const
21 {
22     return m_id;
23 }
24
25 QString Category::name() const
26 {
27     return m_categoryName;
28 }
29
30 void Category::setId(const QString& id)
31 {
32     m_id = id;
33 }
34
35 void Category::setName (const QString& name)
36 {
37     m_categoryName = name;
38 }
39
40 int Category::totalApplicationCount() const
41 {
42     return m_totalApplicationCount;
43 }
44
45 ApplicationList Category::applicationList()
46 {
47 #if 0
48     // update cache
49     if (!applicationList.isEmpty()) {
50         ApplicationList appsToResolve;
51         appsToResolve = ApplicationManager::filterAppsToResolve(applicationList);
52         if (!appsToResolve.isEmpty()) {
53             ApplicationManager::instance()->resolve(appsToResolve);
54         }
55     }
56 #endif
57
58     if (!m_applicationListRequested && !m_internalRequest) {
59         ApplicationSearchJob * job = requestApplicationSearch("", SortByName, 0, 100);
60         connect(job, SIGNAL(jobOver()), job, SLOT(deleteLater()));
61         job->start();
62         m_applicationListRequested = true;
63         m_internalRequest = job;
64     }
65
66     return m_applicationList;
67 }
68
69 ApplicationSearchJob * Category::requestApplicationSearch(
70         const QString &searchString,
71         ApplicationListSortType sortType,
72         int pageNumber,
73         int itemsPerPage)
74 {
75     ApplicationSearchJob * job = m_catalog->requestApplicationSearch(searchString, sortType, CategoryList() << this, pageNumber, itemsPerPage);
76     connect(job, SIGNAL(jobOver()), SLOT(onApplicationSearchOver()));
77     return job;
78 }
79
80 void Category::onApplicationSearchOver()
81 {
82     gcsDebug();
83
84     ApplicationSearchJob *searchJob = static_cast<ApplicationSearchJob*> (sender());
85     if (!searchJob || !searchJob->isSuccessful()) {
86         return;
87     }
88     
89     mergeApplicationList(searchJob->result());
90     m_totalApplicationCount = searchJob->totalItems();
91
92     // get more pages if exist
93     if (m_internalRequest == searchJob) {
94         m_internalRequest = m_internalRequest->requestNextPage();
95         if (m_internalRequest) {
96             gcsDebug() << "requested next page";
97             connect(m_internalRequest, SIGNAL(jobOver()), SLOT(onApplicationSearchFinished()));
98             connect(m_internalRequest, SIGNAL(jobOver()), m_internalRequest, SLOT(deleteLater()));
99             m_internalRequest->start();
100         }
101     }
102 }
103
104 bool Category::mergeApplicationList(const ApplicationList &applicationList)
105 {
106     gcsDebug();
107
108     bool updatedApplications = false;
109     Application * application;
110     foreach(application, applicationList) {
111         updatedApplications |= mergeApplication(application, false, false);
112     }
113
114     sortApplications();
115
116     if (updatedApplications) {
117         emit applicationListUpdated();
118     }
119
120     return updatedApplications;
121 }
122
123 bool Category::mergeApplication(Application *application, bool sort, bool emitUpdated)
124 {
125     bool updatedApplications = false;
126
127     Application * foundApplication = findApplication(application->name());
128     if (!foundApplication) {
129         m_applicationList.append(application);
130         updatedApplications = true;
131     } else {
132         //foundApplication->mergeApplication(application);
133     }
134
135     if (sort) {
136         sortApplications();
137     }
138
139     if (updatedApplications && emitUpdated) {
140         emit applicationListUpdated();
141     }
142
143     return updatedApplications;
144 }
145
146 Application *Category::findApplication(const QString & name) const
147 {
148     Application * application;
149     foreach(application, m_applicationList) {
150         if (application->name() == name) {
151             return application;
152         }
153     }
154
155     return 0;
156 }
157
158 bool Category::mergeCategory(Category * category)
159 {
160     if (name() != category->name()) {
161         return false;
162     }
163
164     mergeApplicationList(category->applicationList());
165
166     return true;
167 }
168
169 void Category::sortApplications()
170 {
171     m_applicationList = Application::sortApplications(m_applicationList, MeeGoGarage::SortByName);
172 }
173
174 void Category::dumpCategory()
175 {
176     gcsDebug() << "category: " << m_categoryName;
177
178     Utils::dumpApplicationList(m_applicationList);
179 }