]> rtime.felk.cvut.cz Git - frescor/frsh.git/blob - fres/frm_gui/resourcelistmodel.cpp
GUI: Added missing check for FORB exception
[frescor/frsh.git] / fres / frm_gui / resourcelistmodel.cpp
1 #include <QMessageBox>
2 #include "resourcelistmodel.h"
3 #include <iostream>
4 using namespace std;
5
6 QString resTypeNames[] = {
7     "CPU",
8     "NET",
9     "MEM",
10     "DISK",
11     "FPGA"
12 };
13
14 QString restype2str(unsigned type)
15 {
16     if (type < sizeof(resTypeNames)/sizeof(*resTypeNames)) {
17         return resTypeNames[type];
18     } else
19         return QString("%1").arg(type);
20 }
21
22 ResourceListModel::ResourceListModel(QObject *parent, forb_orb aorb) :
23         QAbstractListModel(parent),
24          orb(aorb), resources(0)
25 {
26 }
27
28 int ResourceListModel::update()
29 {
30     fres_contract_broker fcb;
31     CORBA_Environment ev;
32     fres_resource_seq *old;
33     unsigned i;
34
35     fcb = forb_resolve_reference(orb, fres_contract_broker_reg_name);
36     if (!fcb) {
37         //QMessageBox::warning(0, tr("Warning"), tr("Could not find contract broker."));
38         return -1;
39     }
40
41     old = resources;
42     fres_contract_broker_get_resources(fcb, &resources, &ev);
43     forb_object_release(fcb);
44     if (forb_exception_occurred(&ev)) {
45         return -1;
46     }
47
48     if (!old) {
49         reset();
50     } else {
51         for (i=0; i<resources->_length && i<old->_length; i++) {
52             if (resources->_buffer[i].restype != old->_buffer[i].restype ||
53                 resources->_buffer[i].resid   != old->_buffer[i].resid) {
54                 dataChanged(createIndex(i, 0), createIndex(i, 0));
55             }
56         }
57         if (old->_length != resources->_length) {
58 //                dataChanged(createIndex(min(old->_length, resources->_length), 0),
59 //                            createIndex(max(old->_length, resources->_length), 0));
60             reset();
61         }
62         forb_free(old->_buffer);
63         forb_free(old);
64     }
65
66     return 0;
67 }
68
69 int ResourceListModel::rowCount(const QModelIndex &parent) const
70 {
71     if (resources && !parent.isValid()) {
72         return resources->_length;
73     } else
74         return 0;
75 }
76
77 QVariant ResourceListModel::data(const QModelIndex &index, int role) const
78 {
79     if (resources && index.isValid())
80     {
81         switch (role) {
82             case Qt::DisplayRole:
83             case Qt::EditRole:
84                 return QVariant(QString("%1 (%2.%3)")
85                                 .arg(resources->_buffer[index.row()].desc.name)
86                                 .arg(restype2str(resources->_buffer[index.row()].restype))
87                                 .arg(resources->_buffer[index.row()].resid));
88              case ObjRefRole:
89                 return qVariantFromValue((void*)resources->_buffer[index.row()].desc.manager);
90             }
91     }
92     return QVariant();
93 }
94
95
96 Qt::ItemFlags ResourceListModel::flags(const QModelIndex &index) const
97 {
98      if (!index.isValid())
99          return 0;
100
101      return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
102  }