~x2go/x2go/x2goclient_master

« back to all changes in this revision

Viewing changes to src/sharewidget.cpp

  • Committer: Mihai Moldovan
  • Date: 2015-03-04 20:15:47 UTC
  • Revision ID: git-v1:b7398771a7abd84ddcff407063edb95dd0a205d3
general: move *.cpp and *.h files to src/ and *.ts files to src/i18n/.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**************************************************************************
 
2
*   Copyright (C) 2005-2015 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 <http://www.gnu.org/licenses/>. *
 
16
***************************************************************************/
 
17
 
 
18
#include "sharewidget.h"
 
19
#include "onmainwindow.h"
 
20
 
 
21
#include <QTreeView>
 
22
#include <QStandardItemModel>
 
23
#include <QLabel>
 
24
#include <QPushButton>
 
25
#include <QGroupBox>
 
26
#include <QCheckBox>
 
27
#include <QBoxLayout>
 
28
#include <QHeaderView>
 
29
#include <QComboBox>
 
30
#include <QTextStream>
 
31
#include <QMessageBox>
 
32
 
 
33
#include "x2gologdebug.h"
 
34
 
 
35
#include <QFileDialog>
 
36
#include <QDir>
 
37
#include "x2gosettings.h"
 
38
 
 
39
ShareWidget::ShareWidget ( QString id, ONMainWindow * mw,
 
40
                           QWidget * parent, Qt::WindowFlags f )
 
41
    : ConfigWidget ( id,mw,parent,f )
 
42
{
 
43
    QGroupBox *egb=new QGroupBox ( tr ( "&Folders" ),this );
 
44
    expTv=new QTreeView ( egb );
 
45
    expTv->setItemsExpandable ( false );
 
46
    expTv->setRootIsDecorated ( false );
 
47
 
 
48
    model=new QStandardItemModel ( 0,2 );
 
49
    ldir=new QLabel ( egb );
 
50
 
 
51
 
 
52
    model->setHeaderData ( 0,Qt::Horizontal,QVariant (
 
53
                               ( QString ) tr ( "Path" ) ) );
 
54
    model->setHeaderData ( 1,Qt::Horizontal,QVariant (
 
55
                               ( QString ) tr ( "Automount" ) ) );
 
56
    expTv->setEditTriggers ( QAbstractItemView::NoEditTriggers );
 
57
 
 
58
 
 
59
 
 
60
    QPushButton* openDir=new QPushButton (
 
61
        QIcon ( mainWindow->iconsPath ( "/16x16/file-open.png" ) ),
 
62
        QString::null,egb );
 
63
 
 
64
    QPushButton* addDir=new QPushButton ( tr ( "Add" ),egb );
 
65
    QPushButton* delDir=new QPushButton ( tr ( "Delete" ),egb );
 
66
#ifdef Q_WS_HILDON
 
67
    QSize sz=addDir->sizeHint();
 
68
    sz.setHeight ( ( int ) ( sz.height() /1.5 ) );
 
69
    addDir->setFixedSize ( sz );
 
70
    sz=delDir->sizeHint();
 
71
    sz.setHeight ( ( int ) ( sz.height() /1.5 ) );
 
72
    delDir->setFixedSize ( sz );
 
73
#endif
 
74
 
 
75
    QLabel *dirPrompt=new QLabel ( tr ( "Path:" ),egb );
 
76
    dirPrompt->setFixedSize ( dirPrompt->sizeHint() );
 
77
    openDir->setFixedSize ( openDir->sizeHint() );
 
78
 
 
79
    ldir->setFrameStyle ( QFrame::StyledPanel|QFrame::Sunken );
 
80
 
 
81
    cbFsConv=new QCheckBox (
 
82
        tr ( "Filename encoding"
 
83
           ),egb );
 
84
 
 
85
    QHBoxLayout* enclay=new QHBoxLayout;
 
86
    cbFrom=new QComboBox ( egb );
 
87
    cbTo=new QComboBox ( egb );
 
88
    lFrom=new QLabel ( tr ( "local:" ),egb );
 
89
    lTo=new QLabel ( tr ( "remote:" ),egb );
 
90
 
 
91
    enclay->addWidget ( cbFsConv );
 
92
    enclay->addWidget ( lFrom );
 
93
    enclay->addWidget ( cbFrom );
 
94
    enclay->addWidget ( lTo );
 
95
    enclay->addWidget ( cbTo );
 
96
    enclay->addStretch();
 
97
    loadEnc ( cbFrom );
 
98
    loadEnc ( cbTo );
 
99
 
 
100
    cbFsSshTun=new QCheckBox (
 
101
        tr ( "Use ssh port forwarding to tunnel file system "
 
102
             "connections through firewalls" ),egb );
 
103
 
 
104
    QVBoxLayout* expLay=new QVBoxLayout ( this );
 
105
    expLay->addWidget ( egb );
 
106
 
 
107
    QHBoxLayout *tvLay=new QHBoxLayout ( egb );
 
108
 
 
109
    QHBoxLayout *dirLAy=new QHBoxLayout();
 
110
    dirLAy->addWidget ( dirPrompt );
 
111
    dirLAy->addWidget ( ldir );
 
112
    dirLAy->addWidget ( openDir );
 
113
 
 
114
    QVBoxLayout* leftLay=new QVBoxLayout();
 
115
    leftLay->addLayout ( dirLAy );
 
116
    leftLay->addSpacing ( 10 );
 
117
    leftLay->addWidget ( expTv );
 
118
    expLay->addLayout ( enclay );
 
119
    expLay->addWidget ( cbFsSshTun );
 
120
 
 
121
    QVBoxLayout* rightLay=new QVBoxLayout();
 
122
    rightLay->addWidget ( addDir );
 
123
    rightLay->addStretch();
 
124
    rightLay->addWidget ( delDir );
 
125
    rightLay->addStretch();
 
126
 
 
127
 
 
128
    tvLay->addLayout ( leftLay );
 
129
    tvLay->addSpacing ( 10 );
 
130
    tvLay->addLayout ( rightLay );
 
131
 
 
132
 
 
133
 
 
134
    expTv->setModel ( ( QAbstractItemModel* ) model );
 
135
    QFontMetrics fm1 ( expTv->font() );
 
136
    expTv->header()->resizeSection ( 1,
 
137
                                     fm1.width ( tr ( "Automount" ) ) +10 );
 
138
    connect ( openDir,SIGNAL ( clicked() ),this,SLOT ( slot_openDir() ) );
 
139
    connect ( addDir,SIGNAL ( clicked() ),this,SLOT ( slot_addDir() ) );
 
140
    connect ( delDir,SIGNAL ( clicked() ),this,SLOT ( slot_delDir() ) );
 
141
    connect ( cbFsConv,SIGNAL ( clicked() ),this
 
142
              ,SLOT ( slot_convClicked() ) );
 
143
    readConfig();
 
144
}
 
145
 
 
146
 
 
147
ShareWidget::~ShareWidget()
 
148
{
 
149
}
 
150
 
 
151
void ShareWidget::slot_openDir()
 
152
{
 
153
    QString startDir=ONMainWindow::getHomeDirectory();
 
154
#ifdef Q_OS_WIN
 
155
    if ( ONMainWindow::getPortable() &&
 
156
            ONMainWindow::U3DevicePath().length() >0 )
 
157
    {
 
158
        startDir=ONMainWindow::U3DevicePath() +"/";
 
159
    }
 
160
#endif
 
161
 
 
162
    QString path= QFileDialog::getExistingDirectory (
 
163
                      this,
 
164
                      tr ( "Select folder" ),
 
165
                      startDir );
 
166
    if ( path!=QString::null )
 
167
    {
 
168
#ifdef Q_OS_WIN
 
169
        if ( ONMainWindow::getPortable() &&
 
170
                ONMainWindow::U3DevicePath().length() >0 )
 
171
        {
 
172
            if ( path.indexOf ( ONMainWindow::U3DevicePath() ) !=0 )
 
173
            {
 
174
                QMessageBox::critical (
 
175
                    0l,tr ( "Error" ),
 
176
                    tr ( "x2goclient is running in "
 
177
                         "portable mode. You should "
 
178
                         "use a path on your usb device "
 
179
                         "to be able to access your data "
 
180
                         "whereever you are" ),
 
181
                    QMessageBox::Ok,QMessageBox::NoButton );
 
182
                slot_openDir();
 
183
                return;
 
184
            }
 
185
            path.replace ( ONMainWindow::U3DevicePath(),
 
186
                           "(U3)" );
 
187
        }
 
188
#endif
 
189
        ldir->setText ( path );
 
190
    }
 
191
}
 
192
 
 
193
 
 
194
void ShareWidget::slot_addDir()
 
195
{
 
196
    QString path=ldir->text();
 
197
    if ( path.length() <1 )
 
198
        return;
 
199
    for ( int i=0; i<model->rowCount(); ++i )
 
200
        if ( model->index ( i,0 ).data().toString() ==path )
 
201
            return;
 
202
    QStandardItem *item;
 
203
    item= new QStandardItem ( path );
 
204
    model->setItem ( model->rowCount(),0,item );
 
205
    item= new QStandardItem();
 
206
    item->setCheckable ( true );
 
207
    model->setItem ( model->rowCount()-1,1,item );
 
208
    ldir->setText ( QString::null );
 
209
}
 
210
 
 
211
 
 
212
void ShareWidget::slot_delDir()
 
213
{
 
214
    model->removeRow ( expTv->currentIndex().row() );
 
215
}
 
216
 
 
217
 
 
218
void ShareWidget::readConfig()
 
219
{
 
220
 
 
221
    X2goSettings st ( "sessions" );
 
222
 
 
223
    QString exportDir=st.setting()->value ( sessionId+"/export",
 
224
                                            ( QVariant ) QString::null ).toString();
 
225
 
 
226
    cbFsSshTun->setChecked ( st.setting()->value ( sessionId+"/fstunnel",
 
227
                             true ).toBool() );
 
228
    QStringList lst=exportDir.split ( ";",QString::SkipEmptyParts );
 
229
 
 
230
    QString toCode=st.setting()->value ( sessionId+"/iconvto",
 
231
                                         ( QVariant ) "UTF-8" ).toString();
 
232
 
 
233
#ifdef Q_OS_WIN
 
234
    QString fromCode=st.setting()->value ( sessionId+"/iconvfrom",
 
235
                                           ( QVariant ) tr (
 
236
                                                   "WINDOWS-1252" ) ).toString();
 
237
#endif
 
238
#ifdef Q_OS_DARWIN
 
239
    QString fromCode=st.setting()->value ( sessionId+"/iconvfrom",
 
240
                                           ( QVariant )
 
241
                                           "UTF-8" ).toString();
 
242
#endif
 
243
#ifdef Q_OS_LINUX
 
244
    QString fromCode=st.setting()->value ( sessionId+"/iconvfrom",
 
245
                                           ( QVariant ) tr (
 
246
                                                   "ISO8859-1" ) ).toString();
 
247
#endif
 
248
 
 
249
    cbFsConv->setChecked ( st.setting()->value ( sessionId+"/useiconv",
 
250
                           ( QVariant ) false ).toBool() );
 
251
    slot_convClicked();
 
252
 
 
253
    int ind=cbFrom->findText ( fromCode );
 
254
    if ( ind !=-1 )
 
255
        cbFrom->setCurrentIndex ( ind );
 
256
 
 
257
    ind=cbTo->findText ( toCode );
 
258
    if ( ind !=-1 )
 
259
        cbTo->setCurrentIndex ( ind );
 
260
 
 
261
    for ( int i=0; i<lst.size(); ++i )
 
262
    {
 
263
#ifndef Q_OS_WIN
 
264
        QStringList tails=lst[i].split ( ":",QString::SkipEmptyParts );
 
265
#else
 
266
        QStringList tails=lst[i].split ( "#",QString::SkipEmptyParts );
 
267
#endif
 
268
        QStandardItem *item;
 
269
        item= new QStandardItem ( tails[0] );
 
270
        model->setItem ( model->rowCount(),0,item );
 
271
        item= new QStandardItem();
 
272
        item->setCheckable ( true );
 
273
        if ( tails[1]=="1" )
 
274
            item->setCheckState ( Qt::Checked );
 
275
        model->setItem ( model->rowCount()-1,1,item );
 
276
    }
 
277
}
 
278
 
 
279
void ShareWidget::setDefaults()
 
280
{
 
281
    cbFsSshTun->setChecked ( true );
 
282
 
 
283
    QString toCode="UTF-8";
 
284
 
 
285
#ifdef Q_OS_WIN
 
286
    QString fromCode=tr ( "WINDOWS-1252" );
 
287
#endif
 
288
#ifdef Q_OS_DARWIN
 
289
    QString fromCode="UTF-8";
 
290
#endif
 
291
#ifdef Q_OS_LINUX
 
292
    QString fromCode=tr ( "ISO8859-1" );
 
293
#endif
 
294
 
 
295
    cbFsConv->setChecked ( false );
 
296
    slot_convClicked();
 
297
 
 
298
    int ind=cbFrom->findText ( fromCode );
 
299
    if ( ind !=-1 )
 
300
        cbFrom->setCurrentIndex ( ind );
 
301
    ind=cbTo->findText ( toCode );
 
302
    if ( ind !=-1 )
 
303
        cbTo->setCurrentIndex ( ind );
 
304
}
 
305
 
 
306
 
 
307
void ShareWidget::saveSettings()
 
308
{
 
309
 
 
310
    X2goSettings st ( "sessions" );
 
311
    st.setting()->setValue ( sessionId+"/fstunnel",
 
312
                             ( QVariant ) cbFsSshTun->isChecked() );
 
313
 
 
314
    QString exportDirs;
 
315
    for ( int i=0; i<model->rowCount(); ++i )
 
316
    {
 
317
#ifndef Q_OS_WIN
 
318
        exportDirs+=model->index ( i,0 ).data().toString() +":";
 
319
#else
 
320
        exportDirs+=model->index ( i,0 ).data().toString() +"#";
 
321
#endif
 
322
 
 
323
        if ( model->item ( i,1 )->checkState() ==Qt::Checked )
 
324
            exportDirs+="1;";
 
325
        else
 
326
            exportDirs+="0;";
 
327
    }
 
328
    st.setting()->setValue ( sessionId+"/export", ( QVariant ) exportDirs );
 
329
 
 
330
 
 
331
    st.setting()->setValue ( sessionId+"/iconvto",cbTo->currentText() );
 
332
    st.setting()->setValue ( sessionId+"/iconvfrom",cbFrom->currentText() );
 
333
    st.setting()->setValue ( sessionId+"/useiconv",cbFsConv->isChecked() );
 
334
    st.setting()->sync();
 
335
}
 
336
 
 
337
 
 
338
void ShareWidget::loadEnc ( QComboBox* cb )
 
339
{
 
340
    QFile file ( ":/txt/encodings" );
 
341
    if ( !file.open ( QIODevice::ReadOnly | QIODevice::Text ) )
 
342
        return;
 
343
 
 
344
    QTextStream in ( &file );
 
345
    while ( !in.atEnd() )
 
346
    {
 
347
        QString line = in.readLine();
 
348
        line=line.replace ( "//","" );
 
349
        cb->addItem ( line );
 
350
    }
 
351
}
 
352
 
 
353
void ShareWidget::slot_convClicked()
 
354
{
 
355
    bool val=cbFsConv->isChecked();
 
356
    cbTo->setEnabled ( val );
 
357
    cbFrom->setEnabled ( val );
 
358
    lTo->setEnabled ( val );
 
359
    lFrom->setEnabled ( val );
 
360
}