1 /**************************************************************************
2 *   Copyright (C) 2005-2020 by Oleksandr Shneyder                         *
3 *                              <o.shneyder@phoca-gmbh.de>                 *
4 *                                                                         *
5 *   This program is free software; you can redistribute it and/or modify  *
6 *   it under the terms of the GNU General Public License as published by  *
7 *   the Free Software Foundation; either version 2 of the License, or     *
8 *   (at your option) any later version.                                   *
9 *   This program is distributed in the hope that it will be useful,       *
10 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12 *   GNU General Public License for more details.                          *
13 *                                                                         *
14 *   You should have received a copy of the GNU General Public License     *
15 *   along with this program.  If not, see <https://www.gnu.org/licenses/>. *
16 ***************************************************************************/
17 
18 #include "x2goclientconfig.h"
19 #include "sessionmanagedialog.h"
20 #include "onmainwindow.h"
21 #include "x2gologdebug.h"
22 
23 #include <QPushButton>
24 #include <QDir>
25 #include <QFrame>
26 #include <QBoxLayout>
27 #include <QTreeWidget>
28 #include <QStringListModel>
29 #include <QShortcut>
30 #include "sessionbutton.h"
31 #include "folderbutton.h"
32 #include "sessionexplorer.h"
33 
34 #define SESSIONROLE Qt::UserRole+1
35 #define SESSIONIDROLE Qt::UserRole+2
36 
SessionManageDialog(QWidget * parent,bool onlyCreateIcon,Qt::WindowFlags f)37 SessionManageDialog::SessionManageDialog ( QWidget * parent,
38         bool onlyCreateIcon, Qt::WindowFlags f )
39     : QDialog ( parent, f )
40 {
41     QVBoxLayout* ml=new QVBoxLayout ( this );
42     QFrame *fr=new QFrame ( this );
43     QHBoxLayout* frLay=new QHBoxLayout ( fr );
44 
45     currentPath="";
46 
47     QPushButton* ok=new QPushButton ( tr ( "E&xit" ),this );
48     QHBoxLayout* bLay=new QHBoxLayout();
49 
50     sessions=new QTreeWidget ( fr );
51     frLay->addWidget ( sessions );
52 
53     QPushButton* newSession=new QPushButton ( tr ( "&New session" ),fr );
54     editSession=new QPushButton ( tr ( "&Session preferences" ),fr );
55     removeSession=new QPushButton ( tr ( "&Delete session" ),fr );
56 #if (!defined Q_WS_HILDON) && (!defined Q_OS_DARWIN)
57     if ( !ONMainWindow::getPortable() )
58         createSessionIcon=new QPushButton (
59             tr ( "&Create session icon on desktop ..." ),fr );
60 #endif
61     par= ( ONMainWindow* ) parent;
62     newSession->setIcon ( QIcon (
63                               par->iconsPath ( "/16x16/new_file.png" ) ) );
64     editSession->setIcon ( QIcon (
65                                par->iconsPath ( "/16x16/edit.png" ) ) );
66 #if (!defined Q_WS_HILDON) && (!defined Q_OS_DARWIN)
67     if ( !ONMainWindow::getPortable() )
68         createSessionIcon->setIcon (
69             QIcon ( par->iconsPath ( "/16x16/create_file.png" ) ) );
70 #endif
71     removeSession->setIcon (
72         QIcon ( par->iconsPath ( "/16x16/delete.png" ) ) );
73 
74     QVBoxLayout* actLay=new QVBoxLayout();
75     actLay->addWidget ( newSession );
76     actLay->addWidget ( editSession );
77     actLay->addWidget ( removeSession );
78 #if (!defined Q_WS_HILDON) && (!defined Q_OS_DARWIN)
79     if ( !ONMainWindow::getPortable() )
80         actLay->addWidget ( createSessionIcon );
81 #endif
82     actLay->addStretch();
83     frLay->addLayout ( actLay );
84 
85     if ( onlyCreateIcon )
86     {
87         newSession->hide();
88         editSession->hide();
89         removeSession->hide();
90     }
91 
92     QShortcut* sc=new QShortcut (
93         QKeySequence ( tr ( "Delete","Delete" ) ),this );
94     connect ( ok,SIGNAL ( clicked() ),this,SLOT ( close() ) );
95     connect (
96         sc,SIGNAL ( activated() ),removeSession,SIGNAL ( clicked() ) );
97     connect (
98         removeSession,SIGNAL ( clicked() ),this,SLOT ( slot_delete() ) );
99     connect ( editSession,SIGNAL ( clicked() ),this,SLOT ( slot_edit() ) );
100 #if (!defined Q_WS_HILDON) && (!defined Q_OS_DARWIN)
101     if ( !ONMainWindow::getPortable() )
102         connect ( createSessionIcon,SIGNAL ( clicked() ),
103                   this,SLOT ( slot_createSessionIcon() ) );
104 #endif
105     connect ( newSession,SIGNAL ( clicked() ),this,SLOT ( slotNew() ) );
106     bLay->setSpacing ( 5 );
107     bLay->addStretch();
108     bLay->addWidget ( ok );
109     ml->addWidget ( fr );
110     ml->addLayout ( bLay );
111 
112     fr->setFrameStyle ( QFrame::StyledPanel | QFrame::Raised );
113     fr->setLineWidth ( 2 );
114 
115     setSizeGripEnabled ( true );
116     setWindowIcon (
117         QIcon (
118             ( ( ONMainWindow* ) parent )->iconsPath (
119                 "/32x32/edit.png" ) ) );
120 
121     setWindowTitle ( tr ( "Session management" ) );
122     loadSessions();
123     connect (sessions, SIGNAL (itemPressed (QTreeWidgetItem *, int)),
124              this, SLOT (slot_endisable (QTreeWidgetItem *, int)));
125     connect (sessions, SIGNAL (itemActivated (QTreeWidgetItem *, int)),
126              this, SLOT (slot_endisable (QTreeWidgetItem *, int)));
127     connect (sessions, SIGNAL (itemChanged (QTreeWidgetItem *, int)),
128              this, SLOT (slot_endisable (QTreeWidgetItem *, int)));
129     connect (sessions, SIGNAL (currentItemChanged (QTreeWidgetItem *, QTreeWidgetItem *)),
130              this, SLOT (slot_endisable_ItemChanged_wrapper (QTreeWidgetItem *, QTreeWidgetItem *)));
131     connect (sessions,SIGNAL (itemDoubleClicked (QTreeWidgetItem *, int)),
132              this, SLOT (slot_dclicked (QTreeWidgetItem *, int)));
133 }
134 
135 
~SessionManageDialog()136 SessionManageDialog::~SessionManageDialog()
137 {}
138 
139 
loadSessions()140 void SessionManageDialog::loadSessions()
141 {
142     sessions->clear();
143 
144     /*const QList<SessionButton*> *sess=par->getSessionExplorer()->getSessionsList();*/
145     /*const QList<FolderButton*> *folders=par->getSessionExplorer()->getFoldersList();*/
146 
147     removeSession->setEnabled ( false );
148     editSession->setEnabled ( false );
149 #if (!defined Q_WS_HILDON) && (!defined Q_OS_DARWIN)
150     if ( !ONMainWindow::getPortable() )
151         createSessionIcon->setEnabled ( false );
152 #endif
153 
154     QTreeWidgetItem* root;
155 
156     root=new QTreeWidgetItem(sessions);
157     root->setText(0,"/");
158     root->setIcon(0,QIcon(":/img/icons/128x128/folder.png"));
159     initFolders(root, "");
160     root->setExpanded(true);
161     root->setData(0, SESSIONROLE , false);
162 
163     sessions->setRootIsDecorated(false);
164     sessions->setHeaderHidden(true);
165 }
166 
initFolders(QTreeWidgetItem * parent,QString path)167 void SessionManageDialog::initFolders(QTreeWidgetItem* parent, QString path)
168 {
169     FolderButton* b;
170     foreach(b, *(par->getSessionExplorer()->getFoldersList()))
171     {
172         if(b->getPath()==path)
173         {
174             QTreeWidgetItem* it=new QTreeWidgetItem(parent);
175             it->setText(0,b->getName());
176             it->setIcon(0, QIcon(*(b->folderIcon())));
177             QString normPath=(b->getPath()+"/"+b->getName()).split("/",QString::SkipEmptyParts).join("/");
178             it->setData(0,Qt::UserRole, normPath+"/");
179             it->setData(0, SESSIONROLE , false);
180             initFolders(it, normPath);
181         }
182     }
183     for(int i=0; i< par->getSessionExplorer()->getSessionsList()->count(); ++i)
184     {
185         SessionButton* s=par->getSessionExplorer()->getSessionsList()->at(i);
186         if(s->getPath()==path)
187         {
188             QTreeWidgetItem* it=new QTreeWidgetItem(parent);
189             it->setText(0,s->name());
190             it->setIcon(0, QIcon(*(s->sessIcon())));
191             QString normPath=(s->getPath()+"/"+s->name()).split("/",QString::SkipEmptyParts).join("/");
192             it->setData(0,Qt::UserRole, normPath+"/");
193             it->setData(0, SESSIONROLE, true);
194             it->setData(0, SESSIONIDROLE, i);
195             initFolders(it, normPath);
196         }
197     }
198 }
199 
200 
slot_endisable(QTreeWidgetItem * item,int col)201 void SessionManageDialog::slot_endisable ( QTreeWidgetItem* item, int col)
202 {
203     Q_UNUSED (col);
204 
205     bool isSess=(item && item->data(0, SESSIONROLE).toBool());
206     x2goDebug << "slot_endisable: isSess: " << isSess;
207 
208     if(!isSess)
209     {
210         if(item)
211             currentPath=item->data(0,Qt::UserRole).toString().split("/",QString::SkipEmptyParts).join("/");
212         else
213 	    currentPath="/";
214         x2goDebug << "slot_endisable: no session, currentPath(?): " << currentPath;
215     }
216 
217     removeSession->setEnabled ( isSess );
218     editSession->setEnabled ( isSess );
219 #if (!defined Q_WS_HILDON) && (!defined Q_OS_DARWIN)
220     if ( !ONMainWindow::getPortable() )
221         createSessionIcon->setEnabled ( isSess );
222 #endif
223 }
224 
slot_endisable_ItemChanged_wrapper(QTreeWidgetItem * item,QTreeWidgetItem *)225 void SessionManageDialog::slot_endisable_ItemChanged_wrapper (QTreeWidgetItem *item, QTreeWidgetItem *) {
226   /*
227    * The int parameter of slot_endisable is unused anyway,
228    * just use the default value in this special case.
229    */
230   slot_endisable (item);
231 }
232 
slot_dclicked(QTreeWidgetItem * item,int)233 void SessionManageDialog::slot_dclicked ( QTreeWidgetItem* item, int )
234 {
235     if(item->data(0, SESSIONROLE).toBool())
236         slot_edit();
237 }
238 
239 
slotNew()240 void SessionManageDialog::slotNew()
241 {
242     par->getSessionExplorer()->setCurrrentPath(currentPath);
243     par->slotNewSession();
244     loadSessions();
245 }
246 
247 
slot_edit()248 void SessionManageDialog::slot_edit()
249 {
250     if((! sessions->currentItem()) || (! sessions->currentItem()->data(0, SESSIONROLE).toBool()))
251         return;
252     int ind=sessions->currentItem()->data(0, SESSIONIDROLE).toInt();
253     par->getSessionExplorer()->slotEdit ( par->getSessionExplorer()->getSessionsList()->at ( ind ) );
254     loadSessions();
255 }
256 
slot_createSessionIcon()257 void SessionManageDialog::slot_createSessionIcon()
258 {
259     if((! sessions->currentItem()) || (! sessions->currentItem()->data(0, SESSIONROLE).toBool()))
260         return;
261     int ind=sessions->currentItem()->data(0, SESSIONIDROLE).toInt();
262     par->getSessionExplorer()->slotCreateDesktopIcon ( par->getSessionExplorer()->getSessionsList()->at ( ind ) );
263 }
264 
265 
slot_delete()266 void SessionManageDialog::slot_delete()
267 {
268     if((! sessions->currentItem()) || (! sessions->currentItem()->data(0, SESSIONROLE).toBool()))
269         return;
270     int ind=sessions->currentItem()->data(0, SESSIONIDROLE).toInt();
271     par->getSessionExplorer()->slotDeleteButton ( par->getSessionExplorer()->getSessionsList()->at ( ind ) );
272     loadSessions();
273 }
274