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 "exportdialog.h"
20 #include "editconnectiondialog.h"
21 #include <QBoxLayout>
22 #include <QGroupBox>
23 #include <QPushButton>
24 #include <QLabel>
25 #include "x2gosettings.h"
26 #include <QListView>
27 #include <QDir>
28 #include <QStringListModel>
29 #include <QShortcut>
30 #include "sessionbutton.h"
31 #include "onmainwindow.h"
32 #include <QFileDialog>
33 #include "sessionexplorer.h"
34 
ExportDialog(QString sid,QWidget * par,Qt::WindowFlags f)35 ExportDialog::ExportDialog ( QString sid,QWidget * par, Qt::WindowFlags f )
36     : QDialog ( par,f )
37 {
38     sessionId=sid;
39     QVBoxLayout* ml=new QVBoxLayout ( this );
40     QFrame *fr=new QFrame ( this );
41     QHBoxLayout* frLay=new QHBoxLayout ( fr );
42 
43     parent= ( ONMainWindow* ) par;
44 
45     QPushButton* cancel=new QPushButton ( tr ( "&Cancel" ),this );
46     QHBoxLayout* bLay=new QHBoxLayout();
47 
48     sessions=new QListView ( fr );
49     frLay->addWidget ( sessions );
50 
51     exportDir=new QPushButton ( tr ( "&share" ),fr );
52     editSession=new QPushButton ( tr ( "&Preferences ..." ),fr );
53     newDir=new QPushButton ( tr ( "&Custom folder ..." ),fr );
54 
55     if(X2goSettings::centralSettings())
56     {
57         editSession->setEnabled(false);
58         editSession->setVisible(false);
59     }
60 
61     QVBoxLayout* actLay=new QVBoxLayout();
62     actLay->addWidget ( exportDir );
63     actLay->addWidget ( editSession );
64     actLay->addWidget ( newDir );
65     actLay->addStretch();
66     frLay->addLayout ( actLay );
67 
68     QShortcut* sc=new QShortcut ( QKeySequence ( tr ( "Delete","Delete" ) ),
69                                   this );
70     connect ( cancel,SIGNAL ( clicked() ),this,SLOT ( close() ) );
71     connect ( sc,SIGNAL ( activated() ),exportDir,SIGNAL ( clicked() ) );
72     connect ( editSession,SIGNAL ( clicked() ),this,SLOT ( slot_edit() ) );
73     connect ( newDir,SIGNAL ( clicked() ),this,SLOT ( slotNew() ) );
74     connect ( exportDir,SIGNAL ( clicked() ),this,SLOT ( slot_accept() ) );
75     bLay->setSpacing ( 5 );
76     bLay->addStretch();
77     bLay->addWidget ( cancel );
78     ml->addWidget ( fr );
79     ml->addLayout ( bLay );
80 
81     fr->setFrameStyle ( QFrame::StyledPanel | QFrame::Raised );
82     fr->setLineWidth ( 2 );
83 
84     setSizeGripEnabled ( true );
85     setWindowTitle ( tr ( "Share Folders" ) );
86     connect ( sessions,SIGNAL ( clicked ( const QModelIndex& ) ),
87               this,SLOT ( slot_activated ( const QModelIndex& ) ) );
88     connect ( sessions,SIGNAL ( doubleClicked ( const QModelIndex& ) ),
89               this,SLOT ( slot_dclicked ( const QModelIndex& ) ) );
90     loadSessions();
91 }
92 
93 
~ExportDialog()94 ExportDialog::~ExportDialog()
95 {}
96 
loadSessions()97 void ExportDialog::loadSessions()
98 {
99     QStringListModel *model= ( QStringListModel* ) sessions->model();
100     if ( !model )
101         model=new QStringListModel();
102     sessions->setModel ( model );
103 
104     QStringList dirs;
105     model->setStringList ( dirs );
106 
107     X2goSettings st ( "sessions" );
108 
109 
110     QString exports=st.setting()->value ( sessionId+"/export",
111                                           ( QVariant ) QString::null ).toString();
112 
113     QStringList lst=exports.split ( ";",QString::SkipEmptyParts );
114     for ( int i=0; i<lst.size(); ++i )
115     {
116 #ifndef Q_OS_WIN
117         QStringList tails=lst[i].split ( ":",QString::SkipEmptyParts );
118 #else
119         QStringList tails=lst[i].split ( "#",QString::SkipEmptyParts );
120 #endif
121         dirs<<tails[0];
122     }
123 
124 
125     model->setStringList ( dirs );
126 
127 
128     //     removeSession->setEnabled(false);
129     exportDir->setEnabled ( false );
130     sessions->setEditTriggers ( QAbstractItemView::NoEditTriggers );
131 }
132 
133 
slot_activated(const QModelIndex &)134 void ExportDialog::slot_activated ( const QModelIndex& )
135 {
136     //     removeSession->setEnabled(true);
137     exportDir->setEnabled ( true );
138 }
139 
slot_dclicked(const QModelIndex &)140 void ExportDialog::slot_dclicked ( const QModelIndex& )
141 {
142     slot_accept();
143 }
144 
145 
slotNew()146 void ExportDialog::slotNew()
147 {
148     directory=QString::null;
149     directory= QFileDialog::getExistingDirectory (
150                    this,
151                    tr ( "Select folder" ),
152                    QDir::homePath() );
153 
154     if ( directory!=QString::null )
155         accept();
156 
157 }
158 
159 
slot_edit()160 void ExportDialog::slot_edit()
161 {
162     const QList<SessionButton*>* sess=parent->getSessionExplorer()->getSessionsList();
163     for ( int i=0; i< sess->size(); ++i )
164     {
165         if ( sess->at ( i )->id() ==sessionId )
166         {
167             parent->getSessionExplorer()->exportsEdit ( sess->at ( i ) );
168             break;
169         }
170     }
171     loadSessions();
172 }
173 
174 
175 
slot_accept()176 void ExportDialog::slot_accept()
177 {
178     int ind=sessions->currentIndex().row();
179     if ( ind<0 )
180         return;
181     QStringListModel *model= ( QStringListModel* ) sessions->model();
182     directory=model->stringList() [ind];
183     accept();
184 }
185