/
qmlaccess.cpp
222 lines (175 loc) · 6.47 KB
/
qmlaccess.cpp
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
#include "qmlaccess.h"
#include <QDebug>
#include <QStringList>
QMLAccess::QMLAccess(QWidget *parent) :
QDeclarativeView(parent)
{
m_context = rootContext();
m_context->setContextProperty("wMain", this);
setAttribute(Qt::WA_OpaquePaintEvent);
setAttribute(Qt::WA_NoSystemBackground);
setSource(QUrl("qrc:/qml/main.qml"));
getContentOf("/");
}
void QMLAccess::debug(QString msg){
qDebug() << msg;
}
void QMLAccess::enterDirectory(QString destination){
getContentOf(destination);
}
void QMLAccess::leaveDirectory(QString actualDir){
if (actualDir.length()>1){
/*** Calculate parent directory ***/
//debug("Actual dir: "+actualDir);
//debug("Actual dir /'s last index "+QString::number(actualDir.lastIndexOf("/")));
QString destination = actualDir.mid(0,actualDir.lastIndexOf("/"));
/*** Get content of directory ***/
if (destination.length()==0){
getContentOf("/");
}else{
getContentOf(destination);
}
}
}
QString QMLAccess::shortname(QString destination){
//debug("Shortname of "+destination);
if (destination.length()>1){
int x = destination.lastIndexOf("/");
return destination.mid(x+1);
}else{
return destination;
}
}
QString QMLAccess::parentDir(QString destination){
if (destination.length()>1){
int x = destination.lastIndexOf("/");
return destination.mid(0,x);
}else{
return destination;
}
}
void QMLAccess::getContentOf(QString destination){
/*** Get the content (dirs and entries) of a directory ***/
QList<QObject*> dataList;
//debug("Getting content of "+destination);
/*** Create a GConfItem to access dirs ***/
GConfItem readDir(destination);
/*** Create a list with all dirs and a list with all entries ***/
QStringList dirs = readDir.listDirs();
QStringList entries = readDir.listEntries();
//debug("Total "+destination+" Dirs: "+QString::number(dirs.count()));
//debug("Total "+destination+" Entries: "+QString::number(entries.count()));
for (int var = 0; var < dirs.count(); ++var) {
//debug("Dir: "+dirs.at(var));
if (destination=="/"){
if (dirs.at(var)!="/schemas"){
dataList.append(new Entity(dirs.at(var),"false",""));
}
}else{
dataList.append(new Entity(dirs.at(var),"false",""));
}
}
for (int var = 0; var < entries.count(); ++var) {
//debug("Entry: "+entries.at(var));
GConfItem entry(entries.at(var));
/*** Get the key's data type using QMetaType ***/
/*** http://developer.qt.nokia.com/doc/qt-4.8/qmetatype.html#Type-enum ***/
/*
Void = 0
Bool = 1
Int = 2
Double = 6
QString = 10
QStringList = 11
*/
int type = entry.value().type();
debug("Type: "+QString::number(type));
switch(type){
case 0:
dataList.append(new Entity(entries.at(var),"true","Void"));
break;
case 1:
dataList.append(new Entity(entries.at(var),"true","Bool"));
break;
case 2:
dataList.append(new Entity(entries.at(var),"true","Int"));
break;
case 6:
dataList.append(new Entity(entries.at(var),"true","Double"));
break;
case 10:
dataList.append(new Entity(entries.at(var),"true","String"));
break;
case 11:
dataList.append(new Entity(entries.at(var),"true","StringList"));
break;
}
}
m_context->setContextProperty("actualDir", destination);
m_context->setContextProperty("gconfKeysModel", QVariant::fromValue(dataList));
}
void QMLAccess::getBoolKey(QString key){
GConfItem entry(key);
m_context->setContextProperty("keyName", shortname(key));
m_context->setContextProperty("boolValue", entry.value().toBool());
}
void QMLAccess::unsetKey(QString key){
GConfItem entry(key);
entry.unset();
}
void QMLAccess::setKey(QString key, QVariant value){
GConfItem entry(key);
entry.set(value);
}
void QMLAccess::getStringKey(QString key){
GConfItem entry(key);
m_context->setContextProperty("keyName", shortname(key));
m_context->setContextProperty("stringValue", entry.value().toString());
}
void QMLAccess::getStringListKey(QString key){
GConfItem entry(key);
QStringList entries = entry.value().toStringList();
currentListValues.clear();
for (int var = 0; var < entries.count(); ++var) {
currentListValues.append(new ListKey(entries.at(var),QString::number(var)));
}
m_context->setContextProperty("keyName", shortname(key));
m_context->setContextProperty("listKeys", QVariant::fromValue(currentListValues));
}
void QMLAccess::removeValueFromList(int index){
currentListValues.removeAt(index);
m_context->setContextProperty("listKeys", QVariant::fromValue(currentListValues));
}
void QMLAccess::addValueToList(){
if (currentListValues.count()>1){
QString x = currentListValues.last()->property("index").toString();
currentListValues.append(new ListKey("",x));
}else{
currentListValues.append(new ListKey("","0"));
}
m_context->setContextProperty("listKeys", QVariant::fromValue(currentListValues));
}
void QMLAccess::modifyContentOfList(QString newValue, int index){
/*FIXME: Anda pues, actualiza el valor, necesitas el index!*/
//currentListValues.removeAt(index);
currentListValues.at(index)->setProperty("value",newValue);
//currentListValues.insert(index,);
}
void QMLAccess::setListKeys(QString key){
GConfItem entry(key);
QStringList values;
for (int var = 0; var < currentListValues.count(); ++var) {
values.append(currentListValues.at(var)->property("value").toString());
}
entry.set(values);
}
void QMLAccess::getIntKey(QString key){
GConfItem entry(key);
m_context->setContextProperty("keyName", shortname(key));
m_context->setContextProperty("intValue", entry.value().toString());
}
void QMLAccess::getDoubleKey(QString key){
GConfItem entry(key);
m_context->setContextProperty("keyName", shortname(key));
m_context->setContextProperty("doubleValue", entry.value().toString());
}