-
Notifications
You must be signed in to change notification settings - Fork 0
/
menuprogrammes.cpp
executable file
·400 lines (299 loc) · 11.9 KB
/
menuprogrammes.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/***************************************************************************
* elokab Copyright (C) 2014 AbouZakaria <yahiaui@gmail.com> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "menuprogrammes.h"
#include "utils/desktopfile.h"
#include "openexec.h"
#include "utils/edir.h"
#include <QFile>
#include <QDir>
#include <QTextStream>
#include <QDebug>
#include <QProcess>
#include <QApplication>
XdgAction::XdgAction(const QString &file,QObject *parent): QAction(parent)
{
DesktopFile dsg(file,"Desktop Entry");
QString iconName=dsg.value("Icon").toString();
setIcon(QIcon::fromTheme(iconName));
setText(dsg.value("Name").toString());
setToolTip(dsg.value("GenericName").toString());
setData(file);
mExec=dsg.value("Exec").toString();
mIconName=iconName;
connect(this,SIGNAL(triggered()),this,SLOT(lanchApplication()));
}
void XdgAction::lanchApplication()
{
QString exec=mExec;
// kdesudo
if(exec.contains("kdesudo")||exec.contains("gksudo")){
exec.remove("kdesudo") ;
exec.remove("gksudo") ;
exec=QString("ekbsudo %1 -i %2").arg(exec).arg(mIconName);
}
QProcess process;
process.setWorkingDirectory(QDir::homePath());
qDebug()<<"XdgAction::lanchApplication()===================="<<exec;
process.startDetached(exec);
}
MenuProgrammes::MenuProgrammes(QWidget *parent) :
QMenu(parent)
{
QStringList list= openDesktopDerectory("elokab-menu-applications.directory");
QString text=list.at(0);
QString icon=list.at(2);
setTitle(text);
// QIcon icnF=EIcon::fromTheme(windowIconText(),"folder");
// setIcon(icnF);
setWindowIconText(icon);
m_menuGen =new MenuGen(this->locale().name().section("_",0,0),this);
connect(m_menuGen,SIGNAL(appDesktopChanged()) ,this,SLOT(rechargeAppDesktop()));
chargeMenu();
}
//! [0]
void MenuProgrammes::chargeMenu()
{
//m_sWatcher->blockSignals(true);
foreach (QMenu *mn,hashMenu){
foreach (QAction *a, mn->actions()) {
delete a;
}
delete mn;
}
menus.clear();
this->clear();
hashMenu.clear();
QString xmlPath=QDir::homePath()+"/.config/menus/elokab-applications.menu";
if(!QFile::exists(xmlPath)){
QDir appDir(QApplication::applicationDirPath());
appDir.cdUp();
appDir.cdUp();
xmlPath= appDir.absolutePath()+"/etc/xdg/menus/elokab-applications.menu";
// xmlPath="/etc/xdg/menus/elokab-applications.menu";xfce-applications.menu
}
QFile file(xmlPath);
qDebug()<<file.fileName()<<"===================";
if (!file.open(QIODevice::ReadOnly)){
qDebug()<<file.errorString();
return;
}
if (!m_doc.setContent(&file)) {
qDebug()<<"MenuProgrammes::chargeMenu() error xmlContent";
file.close();
return;
}
file.close();
// print out the element names of all elements that are direct children
// of the outermost element.
QDomElement docElem = m_doc.documentElement();
QDomNode n = docElem.firstChild();
while(!n.isNull()) {
QDomElement menu = n.toElement(); // try to convert the node to an element.
if(!menu.isNull()) {
// qDebug()<<menu.tagName(); // the node really is an element.
if(menu.tagName()=="Menu"){
// QString name= menu.firstChildElement("Name").text();
QString directory= menu.firstChildElement("Directory").text().trimmed();
QString category;
QDomNode Include = menu.firstChildElement("Include");
QDomNode And = Include.firstChildElement("And");
QDomElement Cat = And.toElement();
if(!Cat.isNull()) {
category= Cat.firstChildElement("Category").text().trimmed();
}
addMenuDirectory(directory,category);
}
}
n = n.nextSibling();
}
chargeAppDesktop();
// m_sWatcher->blockSignals(false);
}
//! [0][1]
void MenuProgrammes::addMenuDirectory( QString directory,QString category)
{
QStringList list= openDesktopDerectory( directory);
QString text=list.at(0);
QString comment=list.at(1);
QString icon=list.at(2);
QMenu *menu=new QMenu;
menu->setTitle(text);
// menu->setToolTip(comment);
//حفظ اسم الايقونة لاستخدامه عند التحديث
menu->setObjectName(icon);
menus.append(menu);
hashMenu.insert(category,menu);
//hashMenu[category] = menu;
QIcon icnF=QIcon::fromTheme(icon,QIcon::fromTheme("folder"));
menu->setIcon(icnF);
}
QStringList MenuProgrammes::openDesktopDerectory(QString directory)
{
QString directoryPath=QDir::homePath()+"/.local/share/desktop-directories/"+directory;
// qDebug()<<directoryPath<<"----------------------------------------------------------------";
if(!QFile::exists(directoryPath)){
QDir appDir(QApplication::applicationDirPath());
appDir.cdUp();
directoryPath= appDir.absolutePath()+"/share/desktop-directories/"+directory;
}
QString lc=this->locale().name().section("_",0,0);
DesktopFile xdg(directoryPath,"Desktop Entry",lc);
QString oldname=xdg.value("Name").toString();
QString text=xdg.value("Name["+lc+"]",oldname).toString();
QString icon=xdg.value("Icon").toString();
QString oldComment=xdg.value("Comment").toString();
QString comment=xdg.value("Comment["+lc+"]",oldComment).toString();
// qDebug()<<directoryPath<<"--------------------------noexist--------------------------------------";
// QString lc=this->locale().name().section("_",0,0);
// QSettings settings(directoryPath,QSettings::IniFormat);
// settings.setIniCodec("UTF-8");
// settings.beginGroup("Desktop Entry");
// QString textOld=settings.value("Name").toString();
// QString text=settings.value("Name["+lc+"]",textOld).toString();
// QString commentOld=settings.value("Comment").toString();
// QString comment=settings.value("Comment["+lc+"]",commentOld).toString();
// QString icon=settings.value("Icon").toString();
// settings.endGroup();
// qDebug()<<text<<comment<<icon<<"----------------------------------------------------------------";
return QStringList()<<text<<comment<<icon;
}
//! [0][2]
void MenuProgrammes::chargeAppDesktop()
{
/**********************************************
*******************new menu*************
**********************************************/
QFile file(Edir::configDir()+"/applictions");
if(!file.exists()||QFileInfo(file).size()==0)
m_menuGen->genirateAppFile();
if (!file.open(QFile::ReadOnly)){
qDebug()<<file.fileName()+file.errorString();
return ;
}
QTextStream textStream(&file);
QString line;
QString lc=this->locale().name().section("_",0,0);
line= textStream.readLine();//premier line
while (!line.isNull()) {
QString filePath=line.section("|",0,0);
DesktopFile dsg(filePath,"Desktop Entry",lc);
QString oldname=dsg.value("Name").toString();
QString name=dsg.value("Name["+lc+"]",oldname).toString();
QString exec=dsg.value("Exec").toString().trimmed();
QString category=dsg.value("Categories","Other").toString().trimmed();
QString icon=dsg.value("Icon").toString();
QString oldgenericName=dsg.value("GenericName").toString();
QString genericName=dsg.value("GenericName["+lc+"]",oldgenericName).toString();
QString oldcomment=dsg.value("Comment",genericName).toString();
QString comment=dsg.value("Comment["+lc+"]",oldcomment).toString();
QAction *act=new QAction(this);
QStringList list;
QRegExp ex("\\%[A-Z]");
QRegExp ex2("\\%[a-z]");
exec.remove(ex);
exec.remove(ex2);
exec.remove("-caption");
list<<exec<<icon.trimmed()<<filePath;
act->setData(list);
act->setText(name);
act->setToolTip(comment);
act->setIcon(QIcon::fromTheme(icon,QIcon::fromTheme("application-x-executable")));
connect(act, SIGNAL(triggered()), this, SLOT(lanchApplication()));
QStringList listCat=category.split(";", QString::SkipEmptyParts);
bool exist=false;
foreach (QMenu *mn,hashMenu){
QString cat=hashMenu.key(mn);
foreach(QString s,listCat){
if(s.contains(cat)&&exist==false){
exist=true;
mn->addAction(act);
}
}
}
if(exist==false){
if(hashMenu.contains("Other")){
QMenu *mn=hashMenu.value("Other");
mn->addAction(act);
}
}
line= textStream.readLine();
}
file.close();
foreach (QMenu *mn,menus){
if(mn->actions().count()>0){
this->addMenu(mn);
}else{
menus.removeOne(mn);
}
}
}
void MenuProgrammes::refreshIcons()
{
// setIcon(EIcon::fromTheme(windowIconText(),"folder"));
QAction *act;
QList<QAction *> list;
foreach (QMenu *mn,menus){
list.append(mn->actions());
QString icon= mn->objectName();
mn->setIcon(QIcon::fromTheme(icon,QIcon::fromTheme("folder")));
}
foreach(act,list)
{
QStringList listAct=act->data().toStringList();
QString icon=listAct.at(1);
QIcon mIcon=QIcon::fromTheme(icon,QIcon::fromTheme("application-x-executable"));
act->setIcon(mIcon);
}
list.clear();
}
void MenuProgrammes::lanchApplication()
{
QAction *action = qobject_cast<QAction *>(sender());
if (action){
QStringList data=action->data().toStringList();
QString exec=data.at(2).trimmed();
qDebug()<<exec;
OpenExec::execFile(exec);
}
emit actionExecuted(action);
// // qDebug()<<"===================="<<action->data().toStringList();
// QStringList data=action->data().toStringList();
// QString exec=data.at(0).trimmed();
// QString terminal=data.at(3).trimmed();
// if(exec.contains("su-to-root")){
// exec=QString("ekbsudo %1 -i %2").arg(exec).arg(data.at(1));
// }
// QProcess process;
// process.setWorkingDirectory(QDir::homePath());
// if(terminal=="true"){
// process.startDetached(QString("xfce4-terminal -e %1").arg(exec));
// }else
// process.startDetached(exec,QStringList(),QDir::homePath());
// // QDesktopServices::openUrl( QUrl::fromLocalFile(exec) );
// emit actionExecuted(action);
// }
}
void MenuProgrammes::rechargeAppDesktop()
{
m_menuGen->blockSignals(true);
chargeMenu();
m_menuGen->blockSignals(false);
emit menuRecharged();
qDebug()<<"MenuProgrammes <<< emit menuRecharged====================================";
}