]> rtime.felk.cvut.cz Git - frescor/frsh.git/blob - fres/frm_gui/resourcelistmodel.cpp
493619f954769e233934ac9ae540fc501f41fbc4
[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
45     if (!old) {
46         reset();
47     } else {
48         for (i=0; i<resources->_length && i<old->_length; i++) {
49             if (resources->_buffer[i].restype != old->_buffer[i].restype ||
50                 resources->_buffer[i].resid   != old->_buffer[i].resid) {
51                 dataChanged(createIndex(i, 0), createIndex(i, 0));
52             }
53         }
54         if (old->_length != resources->_length) {
55 //                dataChanged(createIndex(min(old->_length, resources->_length), 0),
56 //                            createIndex(max(old->_length, resources->_length), 0));
57             reset();
58         }
59         forb_free(old->_buffer);
60         forb_free(old);
61     }
62
63     return 0;
64 }
65
66 int ResourceListModel::rowCount(const QModelIndex &parent) const
67 {
68     if (resources && !parent.isValid()) {
69         return resources->_length;
70     } else
71         return 0;
72 }
73
74 QVariant ResourceListModel::data(const QModelIndex &index, int role) const
75 {
76     if (resources && index.isValid())
77     {
78         switch (role) {
79             case Qt::DisplayRole:
80             case Qt::EditRole:
81                 return QVariant(QString("%1 (%2.%3)")
82                                 .arg(resources->_buffer[index.row()].desc.name)
83                                 .arg(restype2str(resources->_buffer[index.row()].restype))
84                                 .arg(resources->_buffer[index.row()].resid));
85              case ObjRefRole:
86                 return qVariantFromValue((void*)resources->_buffer[index.row()].desc.manager);
87             }
88     }
89     return QVariant();
90 }
91
92
93 Qt::ItemFlags ResourceListModel::flags(const QModelIndex &index) const
94 {
95      if (!index.isValid())
96          return 0;
97
98      return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
99  }