-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
266 lines (232 loc) · 10.2 KB
/
mainwindow.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QDirIterator>
#include <QFileInfo>
#include <QMap>
#include <QStringList>
#include "env.h"
#include <QDir>
#include <QTextStream>
#include <QMessageBox>
#include <QInputDialog>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->actionQuit,SIGNAL(triggered()),this,SLOT(close()));
connect(ui->actionAbout,SIGNAL(triggered()),this,SLOT(showaboutmb()));
cats=new QMap<QString,QMap<QString,QPair<QString, bool> > >; //contents of all .desktop files is stored here
QStringList pathes; //search pathes of .desktop files
pathes.append(default_path);
pathes.append(QDir::homePath()+"/.local/share/applications");
for(QStringList::Iterator it2=pathes.begin();it2!=pathes.end();++it2){
for(QDirIterator it(*it2,QDirIterator::Subdirectories);it.hasNext();it.next()){
if(QFileInfo(it.filePath()).isFile() && QFileInfo(it.filePath()).suffix()=="desktop"){ //.desktop file found
QFile file(it.filePath());
if(file.open(QIODevice::ReadOnly)) {
QTextStream in(&file);
QString entry=default_entry; //setting defaults to obtain errors on empty files
QString name=default_name;
QString cat=default_cat;
bool hidden=false;
while(!in.atEnd()){
QString line=in.readLine();
if(line.size()==0 || line[0]=='#'){
//just skip comment, nothing to do here
}else if(line[0]=='['){
entry=line; //remember current entry
}else{
QString locale_id=default_locale;
QString param;
int iob;
if((iob=line.indexOf('['))!=-1 && iob<line.indexOf('=')){
locale_id=line.mid(iob+1,line.indexOf(']')-iob-1);
param=line.left(iob);
}else{
param=line.left(line.indexOf('='));
}
QString value=line.mid(line.indexOf('=')+1);
if(locale_id==default_locale && entry==default_entry){ //remember default entry & default locale settings
if(param=="Name" && !value.isEmpty()){
name=value;
}else if(param=="Categories"){
cat+=value;
}else if(param=="Hidden"){
hidden=(value=="true");
}else{
//unnecessary params, skipping
}
}
}
}
int ioc; //get names of all categories, including default category; add filename to every category which includes it
while((ioc=cat.indexOf(';'))!=-1){
(*cats)[cat.left(ioc).trimmed()][name]=QPair<QString,bool>(it.filePath(),hidden);
cat=cat.mid(ioc+1);
}
}
file.close();
}
}
}
connect(ui->comboCat,SIGNAL(currentIndexChanged(QString)),this,SLOT(switchcat(QString)));
dfile=0;
connect(ui->listCat,SIGNAL(currentTextChanged(QString)),this,SLOT(switchelem(QString)));
connect(ui->buttonEditItem,SIGNAL(clicked()),this,SLOT(execcurelem()));
connect(ui->buttonAddItem,SIGNAL(clicked()),this,SLOT(additem()));
connect(ui->buttonDelItem,SIGNAL(clicked()),this,SLOT(hideitem()));
connect(ui->buttonVisItem,SIGNAL(clicked()),this,SLOT(visitem()));
connect(ui->cbHidden,SIGNAL(toggled(bool)),this,SLOT(switchhiddenshow(bool)));
for(QMap<QString,QMap<QString,QPair<QString, bool> > >::iterator it=cats->begin();it!=cats->end();++it){
ui->comboCat->addItem(it.key());
}
ui->comboCat->setCurrentIndex(0); //workaround to emit signal and fill list properly
}
MainWindow::~MainWindow(){
delete ui;
delete dfile;
}
void MainWindow::switchhiddenshow(bool flag){
for(int i=0;i<ui->listCat->count();++i){
if((*cats)[ui->comboCat->currentText()][ui->listCat->item(i)->text()].second){
ui->listCat->item(i)->setHidden(!flag);
}
}
}
void MainWindow::additem(){
QString name=QInputDialog::getText(this,"File name","New file name").trimmed();
if(!name.isEmpty()){
QString name_f=name;
name_f.remove("/");
if(name_f[0]=='.'){
name_f="_"+name_f;
}
QString filename=default_path+name_f+".desktop";
int index=0;
while(QFile::exists(filename)){
filename=default_path+name_f+QString::number(index)+".desktop";
++index;
}
QString cat="";
if(ui->comboCat->currentIndex()){
cat=ui->comboCat->currentText()+';';
}
if(dfile){
delete dfile;
}
dfile=new EditDFile(filename,name,cat);
dfile->writeToFile(); //create file with choosen name and default params, dfile will be recreated on list item adding/switching
(*cats)[ui->comboCat->currentText()][name]=QPair<QString,bool>(filename,false); //add filename to current and default cats
(*cats)[default_cat_name][name]=QPair<QString,bool>(filename,false);
QListWidgetItem* item=new QListWidgetItem(name);
ui->listCat->addItem(item);
ui->listCat->setCurrentItem(item);
ui->listCat->sortItems();
ui->listCat->scrollToItem(item);
}
}
void MainWindow::hideitem(){
if(dfile){
dfile->makeHidden();
bool flag=(*cats)[ui->comboCat->currentText()][ui->listCat->currentItem()->text()].second;
QString cat=default_cat+dfile->getProp("Categories");
int ioc; //switch status in every cat
while((ioc=cat.indexOf(';'))!=-1){
(*cats)[cat.left(ioc).trimmed()][ui->listCat->currentItem()->text()].second=!flag;
cat=cat.mid(ioc+1);
}
if(flag){
ui->listCat->currentItem()->setTextColor(Qt::black);
ui->listCat->currentItem()->setHidden(false);
}else{
ui->listCat->currentItem()->setTextColor(Qt::gray);
ui->listCat->currentItem()->setHidden(!ui->cbHidden->isChecked());
ui->listCat->setCurrentRow(ui->listCat->currentRow()+!ui->cbHidden->isChecked()); //switch to next element if current is not aviable
}
}
}
void MainWindow::visitem(){
if(dfile){
dfile->makeVisible();
}
}
void MainWindow::switchelem(QString name){
if(dfile){
delete dfile;
}
if(!name.isEmpty()){ //workaround for empty categories bug
dfile=new EditDFile((*cats)[ui->comboCat->currentText()][name].first);
ui->buttonDelItem->setChecked((dfile->getProp("Hidden")=="true"));
ui->buttonVisItem->setChecked((dfile->getProp("NoDisplay")=="true"));
}else{
dfile=0;
}
}
void MainWindow::execcurelem(){
if(dfile){
QString name=dfile->getProp("Name");
if(name.isEmpty()){
name=default_name;
}
QString cat=default_cat+dfile->getProp("Categories");
QString filename=(*cats)[default_cat_name][name].first;
if(dfile->exec()==QDialog::Accepted){
dfile->writeToFile();
int ioc; //after successfully execution, delete current file info from all cats, and add again
while((ioc=cat.indexOf(';'))!=-1){
(*cats)[cat.left(ioc).trimmed()].erase((*cats)[cat.left(ioc).trimmed()].find(name));
if (!(*cats)[cat.left(ioc).trimmed()].size()){
cats->erase(cats->find(cat.left(ioc).trimmed()));
}
cat=cat.mid(ioc+1);
}
name=dfile->getProp("Name");
if(name.isEmpty()){
name=default_name;
}
cat=default_cat+dfile->getProp("Categories");
bool hidden=(dfile->getProp("Hidden")=="true");
while((ioc=cat.indexOf(';'))!=-1){
(*cats)[cat.left(ioc).trimmed()][name]=QPair<QString,bool>(filename,hidden);
cat=cat.mid(ioc+1);
}
int ci=ui->comboCat->currentIndex();
ui->comboCat->clear();
for(QMap<QString,QMap<QString,QPair<QString, bool> > >::iterator it=cats->begin();it!=cats->end();++it){
ui->comboCat->addItem(it.key());
}
ui->comboCat->setCurrentIndex(std::min(ci,ui->comboCat->count()-1)); //workaround for deleting last cat
int li=0;
while(li<ui->listCat->count() && ui->listCat->item(li)->text()!=name){
++li;
}
if(li==ui->listCat->count()){
li=0;
}
ui->listCat->scrollToItem(ui->listCat->item(li));
ui->listCat->setCurrentRow(li);
}
}
}
void MainWindow::switchcat(QString cat_id){
if(!cat_id.isEmpty()){
ui->listCat->clear();
for(QMap<QString,QPair<QString, bool> >::iterator it=(*cats)[cat_id].begin();it!=(*cats)[cat_id].end();++it){
QListWidgetItem* item=new QListWidgetItem(it.key());
if(it.value().second){
item->setTextColor(Qt::gray);
//item->setHidden(!ui->cbHidden->isChecked()); //TODO: Why this not working?
}
ui->listCat->addItem(item);
}
ui->listCat->sortItems();
ui->listCat->setCurrentItem(ui->listCat->item(0));
ui->cbHidden->setChecked(!ui->cbHidden->isChecked()); //workaround for not working setHidden
ui->cbHidden->setChecked(!ui->cbHidden->isChecked());
}
}
void MainWindow::showaboutmb(){
QMessageBox::about(this,"About","Simple Gnome menu editor written on QT5\n\nby Denis Sheremet\nzaycakitayca@xaker.ru\n\nLicense: WTFPL\n\nDevelopment version\n\nIf you like what I do, you may support me\nYandex.money: 41001853988430\nWMR: R379723001067\nbtc: 1GnmDreXwhSPPnxmmkhimVXR8VGQrwiS2f");
}