Added qanimationlabel to show loading icon while loading stuff form server
[meego-garage:garage-netbook-ui.git] / src / categorymodel.cpp
1 #include "garageclientservices.h"
2 #include "catalog.h"
3 #include "categorymodel.h"
4 #include "category.h"
5
6 #define max(a,b) (((a) > (b)) ? (a) : (b))
7
8 using namespace MeeGoGarage;
9
10 Q_DECLARE_METATYPE(QWeakPointer<Category>);
11
12 CategoryModel::CategoryModel(QObject *parent) :
13     QStandardItemModel(parent)
14 {
15     onCatalogUpdated();
16 }
17
18 void CategoryModel::onCatalogUpdated()
19 {
20     GarageClientServices * gcs = GarageClientServices::instance();
21     if (!gcs) {
22         qWarning() << "GarageClientServices instance not available";
23         return;
24     }
25
26     Catalog * catalog = gcs->catalog();
27     if (!catalog) {
28         connect(gcs, SIGNAL(catalogUpdated()), SLOT(onCatalogUpdated()));
29         return;
30     }
31
32     connect(catalog, SIGNAL(categoryListUpdated()), SLOT(onCatalogUpdated()), Qt::UniqueConnection);
33
34     CategoryList categoryList = catalog->categoryList();
35     setCategoryList(categoryList);
36 }
37
38 void CategoryModel::setCategoryList(const CategoryList & categories)
39 {
40     qDebug() << __func__;
41
42     clear();
43
44     QListIterator<Category*> categoryIterator(categories);
45     qDebug() << "count=" << categories.count();
46     while(categoryIterator.hasNext()) {
47         QWeakPointer<Category> category = categoryIterator.next();
48         if (category.isNull()) {
49             qDebug() << "found invalid category";
50         } else {
51             qDebug() << "Category" << category.data()->name();
52             QVariant variant;
53             variant.setValue(category);
54
55             QStandardItem *item = new QStandardItem (category.data()->name());
56             item->setData(variant, Qt::UserRole);
57             item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
58             appendRow(item);
59         }
60     }
61
62     emit dataChanged(index(0,0),
63                      index(max(0,rowCount()-1), max(0,columnCount()-1)));
64 }