summaryrefslogtreecommitdiffstats
path: root/src/sessiontreemodel.cpp
blob: 7b88015131ee9df7739bc65e562f348d51c14623 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include "sessiontreemodel.h"

#include <QFileInfo>
#include <QIcon>
#include <QResource>
#include <QString>
#include <QtDebug>

#include "sessiontreeitem.h"
#include "vsession.h"
#include "sessionsiconholder.h"

SessionTreeModel::SessionTreeModel(QObject *parent)
    : QAbstractItemModel(parent) {
    root_ = new SessionTreeItem("dummy");
    iconHolder = new SessionsIconHolder(*this);
}

SessionTreeModel::~SessionTreeModel() {
    delete root_;
}

int SessionTreeModel::columnCount(const QModelIndex& /*parent*/) const {
    return 1;
}

int SessionTreeModel::rowCount(const QModelIndex &parent) const {
    SessionTreeItem* parentItem;
    if (parent.column() > 0) {
        return 0;
    }

    if (!parent.isValid()) {
        parentItem = root_;
    } else {
        parentItem = static_cast<SessionTreeItem*>(parent.internalPointer());
    }

    return parentItem->childCount();
}

QVariant SessionTreeModel::data(const QModelIndex &index, int role) const {
    if (!index.isValid()) {
        return QVariant();
    }

    SessionTreeItem* item =
            static_cast<SessionTreeItem*>(index.internalPointer());

    const Session* s = item->session();
    if (s) {
        if (role == Qt::DisplayRole)
            return s->shortDescription();
        if (role == Qt::ToolTipRole)
            return s->description();
        if (role == Qt::DecorationRole) {
            if (index.column() == 0) { // TODO: is this line needed?
                QString icon(s->icon());

                if (icon.isEmpty()) {
                    // Nothing...
                } else if (icon.startsWith("http://")) {
                    // try to load icon from url
                    QIcon url_icon = iconHolder->getIcon(QUrl(icon));
                    if (!url_icon.isNull()) {
                        return url_icon;
                    }
                } else if (QFileInfo(icon).isAbsolute()) {
                    // try to load icon from file
                    return QIcon(icon);
                } else {
                    // try to load icon from QResource
                    QIcon res_icon = iconHolder->getIcon(icon);
                    if (!res_icon.isNull()) {
                        return res_icon;
                    }
                }
                // fallback to os icon
                if (s->type() == Session::VSESSION) {
                    const VSession* vs = (VSession*) s;
                    QString os(vs->getAttribute("os", "param").toLower());
                    if (!os.isEmpty()) {
                        QIcon osi = iconHolder->getIcon(os);
                        if (!osi.isNull())
                            return osi;
                        if (os == "dos")
                            return iconHolder->getIcon("dos");
                        if (os.startsWith("windows7"))
                            return iconHolder->getIcon("win7");
                        if (os.startsWith("win31"))
                            return iconHolder->getIcon("win311");
                        if (os.startsWith("windows8"))
                            return iconHolder->getIcon("win8");
                        if (os.startsWith("win2000"))
                            return iconHolder->getIcon("win2000");
                        if (os.startsWith("winxp"))
                            return iconHolder->getIcon("winxp");
                        if (os.startsWith("debian"))
                            return iconHolder->getIcon("debian");
                        if (os.startsWith("ubuntu"))
                            return iconHolder->getIcon("ubuntu");
                        if (os.startsWith("win"))
                            return iconHolder->getIcon("windows");
                        if (os.contains("linux"))
                            return iconHolder->getIcon("linux");
                    }
                    if (vs->imgtype() == VMWARE)
                        return iconHolder->getIcon("vmware");
                    if (vs->imgtype() == VBOX)
                        return iconHolder->getIcon("virtualbox");
                }
            }
        }
    } else if (role == Qt::DisplayRole) {
        // this is a section item (X Sessions / Virtual Sessions)
        return item->text();
    }

    return QVariant();
}

Qt::ItemFlags SessionTreeModel::flags(const QModelIndex &index) const {
    if (!index.isValid()) {
        return 0;
    }

    SessionTreeItem* item =
            static_cast<SessionTreeItem*>(index.internalPointer());

    if (item->session()) {
        return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
    } else {
        return Qt::ItemIsEnabled;
    }
}

QVariant SessionTreeModel::headerData(int /*section*/,
                                      Qt::Orientation /*orientation*/,
                                      int /*role*/) const {
    return QVariant();
}

QModelIndex SessionTreeModel::index(int row, int column,
                                    const QModelIndex &parent) const {
    if (!hasIndex(row, column, parent)) {
        return QModelIndex();
    }

    SessionTreeItem *parentItem;

    if (!parent.isValid()) {
        parentItem = root_;
    } else {
        parentItem = static_cast<SessionTreeItem*>(parent.internalPointer());
    }

    SessionTreeItem *childItem = parentItem->child(row);
    if (childItem) {
        return createIndex(row, column, childItem);
    } else {
        return QModelIndex();
    }
}

QModelIndex SessionTreeModel::parent(const QModelIndex &index) const {
    if (!index.isValid()) {
        return QModelIndex();
    }

    SessionTreeItem *childItem =
            static_cast<SessionTreeItem*>(index.internalPointer());
    SessionTreeItem *parentItem = childItem->parent();

    if (parentItem == root_) {
        return QModelIndex();
    }

    return createIndex(parentItem->row(), 0, parentItem);
}

void SessionTreeModel::addItems(const QList<Session*>& sessions) {
    foreach (Session* s, sessions) {
        root_->appendChild(new SessionTreeItem(s, root_));
    }
}

void SessionTreeModel::addLabelItem(const QString& label) {
    root_->appendChild(new SessionTreeItem(label, root_));
}

void SessionTreeModel::removeItem(const QString& name) {
	for (int i = 0; i < root_->childCount(); ++i) {
		SessionTreeItem* item = root_->child(i);
		if (item->text() == name) {
			root_->removeChild(item);
		}
	}
}

QList<Session*> SessionTreeModel::lookForItem(const QString& label) {
	QList<Session*> result;

	if (label.startsWith("\"")) {
		QString searchTerm = label;
		searchTerm.remove(0, 1);
		if (label.endsWith("\"")) {
			searchTerm.chop(1);
		}
		QList<QString> items;
		items.append(searchTerm);
		for (int i = 0; i < root_->childCount(); ++i) {
			SessionTreeItem* item = root_->child(i);
			if (item->session()->containsKeywords(items)) {
				result.append(const_cast<Session*>(item->session()));
			}
		}
	} else {
		QList<QString> items = label.split(" ", QString::SkipEmptyParts);
		for (int i = 0; i < root_->childCount(); ++i) {
			SessionTreeItem* item = root_->child(i);
			if (item->session()->containsKeywords(items)) {
				result.append(const_cast<Session*>(item->session()));
			}
		}
	}

	return result;

}

void SessionTreeModel::updateView() {
	emit layoutChanged();
}