~bstrong-f/fisysmgr/trunk

« back to all changes in this revision

Viewing changes to main/source/fsmmain.cpp

  • Committer: Barry Strong
  • Date: 2015-05-10 13:05:49 UTC
  • Revision ID: barry@softtechok.com-20150510130549-xlbzzt5slojawh70
Replaced password dialog with credentials dialog. Added minimal about box. Added change mout option value dialog. modified to compile with Qt4 or Qt5.

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
QString FsmMain::fstabHash = "5fb9a04bf237c44085070a924d8cf4a4d5816db7";
25
25
 
26
26
FsmMain::FsmMain(QWidget *parent) : QMainWindow(parent), ui(new Ui::FsmMain) {
27
 
    ui->setupUi(this);
 
27
 
 
28
    QString     ws;
 
29
 
 
30
    startupOK = true;
28
31
    settings = new QSettings(this);
29
32
    sPlayer = new SoundPlayer(this);
30
33
    helperProc = new QProcess(this);
 
34
    ws = "fisysmgr-c0dd6785-2bda-44d8-b979-5bc66c9aca2d";
 
35
    localSocket.connectToServer(ws);
 
36
    if (localSocket.waitForConnected(500)) {
 
37
        QMessageBox::critical(this,tr("FiSysMg - Message"), tr("An instance of FiSysMgr is already started."));
 
38
        startupOK = false;
 
39
        QTimer::singleShot(0, this, SLOT(startup()));
 
40
        return;
 
41
    }
 
42
    localServer = new QLocalServer(this);
 
43
    connect(localServer, SIGNAL(newConnection()), this, SLOT(newLocalConnection()));
 
44
    if (! localServer->listen(ws)) {
 
45
        sPlayer->play("qrc:///sounds/Warning.ogg");
 
46
        QMessageBox::critical(this,tr("FiSysMg - Message"), tr("Unable to create local server"));
 
47
        startupOK = false;
 
48
        QTimer::singleShot(0, this, SLOT(startup()));
 
49
        return;
 
50
    }
 
51
    hProcEnv = QProcessEnvironment::systemEnvironment();
 
52
#ifdef QT_DEBUG
 
53
    hProcEnv.insert("PATH", hProcEnv.value("PATH") +
 
54
               ":/home/barry/Projects/QtProjects/fisysmgr/build-fisysmgrprj-Qt_4_8_6_System_2-Debug/helper/");
 
55
#endif
 
56
    helperProc->setProcessEnvironment(hProcEnv);
 
57
    helperProc->setReadChannel(QProcess::StandardOutput);
 
58
    helperProc->start("pkexec fisysmgr-helper");
 
59
    if (! helperProc->waitForStarted()) {
 
60
        sPlayer->play("qrc:///sounds/Warning.ogg");
 
61
        QMessageBox::critical(this,tr("FiSysMg - Message"), tr("Unable to start helper process"));
 
62
        startupOK = false;
 
63
        QTimer::singleShot(0, this, SLOT(startup()));
 
64
        return;
 
65
    }
 
66
    accessKey = "";
 
67
    while ((! helperProc->canReadLine()) && (helperProc->state() == QProcess::Running)) {
 
68
        helperProc->waitForReadyRead();
 
69
    }
 
70
    if (helperProc->canReadLine())
 
71
        accessKey = helperProc->readLine().trimmed();
 
72
    if (accessKey == "") {
 
73
        startupOK = false;
 
74
        QTimer::singleShot(0, this, SLOT(startup()));
 
75
        return;
 
76
    }
 
77
    ui->setupUi(this);
31
78
    ui->tbUp->setIcon(QIcon::fromTheme("go-up", QIcon(":/icons/go-up.png")));
32
79
    ui->tbDown->setIcon(QIcon::fromTheme("go-down", QIcon(":/icons/go-down.png")));
33
80
    ui->tbEdit->setIcon(QIcon::fromTheme("document-edit", QIcon(":/icons/document-edit.png")));
50
97
    fstabModel->setHeaderData(4, Qt::Horizontal, tr("Dump "));
51
98
    fstabModel->setHeaderData(5, Qt::Horizontal, tr("fsck "));
52
99
    username = qgetenv("USER");
53
 
    workgroup = settings->value("Samba/Workgroup", "WORKGROUP").toString();
54
100
    winWidth = settings->value("MainWindow/Width", 640).toInt();
55
101
    winHeight = settings->value("MainWindow/Height", 480).toInt();
 
102
    curCreds = settings->value("CIFS/Credentials", "").toString();
56
103
    resize(winWidth, winHeight);
 
104
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
57
105
    fstabView->horizontalHeader()->setResizeMode(QHeaderView::Interactive);
 
106
#else
 
107
    fstabView->horizontalHeader()->setSectionResizeMode(QHeaderView::Interactive);
 
108
#endif
58
109
    fstabView->resizeColumnsToContents();
59
110
    QTimer::singleShot(0, this, SLOT(startup()));
60
111
}
61
112
 
62
113
FsmMain::~FsmMain() {
63
 
    winWidth = settings->value("MainWindow/Width", 640).toInt();
64
 
    winHeight = settings->value("MainWindow/Height", 480).toInt();
65
 
    if ((winWidth != width()) || (winHeight != height())) {
66
 
        settings->setValue("MainWindow/Width", width());
67
 
        settings->setValue("MainWindow/Height", height());
 
114
    if (startupOK) {
 
115
        winWidth = settings->value("MainWindow/Width", 640).toInt();
 
116
        winHeight = settings->value("MainWindow/Height", 480).toInt();
 
117
        if ((winWidth != width()) || (winHeight != height())) {
 
118
            settings->setValue("MainWindow/Width", width());
 
119
            settings->setValue("MainWindow/Height", height());
 
120
        }
68
121
    }
69
122
    if (helperProc->state() == QProcess::Running) {
70
123
        if (helperProc->write("close\n") != 6) {
71
 
            sPlayer->play("qrc:///sounds/Warning.mp3");
 
124
            sPlayer->play("qrc:///sounds/Warning.ogg");
72
125
            QMessageBox::warning(this,tr("FiSysMg - Message"),
73
126
                                 tr("Unable to write 'close' to helper process"));
74
127
        }
75
128
        if (! helperProc->waitForFinished()) {
76
 
            sPlayer->play("qrc:///sounds/Warning.mp3");
 
129
            sPlayer->play("qrc:///sounds/Warning.ogg");
77
130
            QMessageBox::warning(this,tr("FiSysMg - Message"), tr("Unable to close helper process"));
78
131
        }
79
132
    }
81
134
}
82
135
 
83
136
void FsmMain::startup() {
84
 
 
85
 
    int         timeout;
86
 
    QString     ws;
87
 
 
88
 
    ws = "fisysmgr-c0dd6785-2bda-44d8-b979-5bc66c9aca2d";
89
 
    localSocket.connectToServer(ws);
90
 
    if (localSocket.waitForConnected(500)) {
91
 
        QMessageBox::critical(this,tr("FiSysMg - Message"), tr("An instance of FiSysMgr is already started."));
92
 
        close();
93
 
        return;
94
 
    }
95
 
    localServer = new QLocalServer(this);
96
 
    connect(localServer, SIGNAL(newConnection()), this, SLOT(newLocalConnection()));
97
 
    if (! localServer->listen(ws)) {
98
 
        sPlayer->play("qrc:///sounds/Warning.mp3");
99
 
        QMessageBox::critical(this,tr("FiSysMg - Message"), tr("Unable to create local server"));
100
 
        close();
101
 
        return;
102
 
    }
103
 
    hProcEnv = QProcessEnvironment::systemEnvironment();
104
 
#ifdef QT_DEBUG
105
 
    hProcEnv.insert("PATH", hProcEnv.value("PATH") +
106
 
               ":/home/barry/Projects/QtProjects/fisysmgr/build-fisysmgrprj-Qt_4_8_6_System_2-Debug/helper/");
107
 
#endif
108
 
    helperProc->setProcessEnvironment(hProcEnv);
109
 
    helperProc->setReadChannel(QProcess::StandardOutput);
110
 
    helperProc->start("pkexec fisysmgr-helper");
111
 
    if (! helperProc->waitForStarted()) {
112
 
        sPlayer->play("qrc:///sounds/Warning.mp3");
113
 
        QMessageBox::critical(this,tr("FiSysMg - Message"), tr("Unable to start helper process"));
114
 
        close();
115
 
        return;
116
 
    }
117
 
    accessKey = "";
118
 
    timeout = 0;
119
 
    do {
120
 
        if ((helperProc->state() != QProcess::Running) || (timeout > 300)) {
121
 
            qDebug("Unable to read from helper process");
122
 
            close();
123
 
            return;
124
 
        } else {
125
 
            if (helperProc->canReadLine()) {
126
 
                accessKey = helperProc->readLine().trimmed();
127
 
            } else {
128
 
                helperProc->waitForReadyRead(10);
129
 
                usleep(100000);
130
 
            }
131
 
        }
132
 
        timeout++;
133
 
    } while (accessKey == "");
 
137
    if (! startupOK) {
 
138
        close();
 
139
        return;
 
140
    }
134
141
    diskInfo.setAccessKey(accessKey);
135
142
    if (! loadfstab()) {
136
143
            close();
165
172
#endif
166
173
    fsFile.setFileName(ws);
167
174
    if (! fsFile.open(QIODevice::ReadOnly)) {
168
 
        sPlayer->play("qrc:///sounds/Warning.mp3");
 
175
        sPlayer->play("qrc:///sounds/Warning.ogg");
169
176
        QMessageBox::warning(this,tr("FiSysMg - Message"), tr("Unable to open file ") + "'" + ws + "'" );
170
177
        return false;
171
178
    }
215
222
 
216
223
void FsmMain::on_action_About_triggered() {
217
224
 
 
225
    AboutBox about(this);
 
226
 
 
227
    about.exec();
218
228
}
219
229
 
220
230
void FsmMain::on_actionBackup_FileSystem_Config_triggered() {
255
265
#endif
256
266
    saveFile.setFileName(fileName);
257
267
    if (! fstabFile.open(QIODevice::ReadOnly|QIODevice::Text)) {
258
 
        sPlayer->play("qrc:///sounds/Warning.mp3");
 
268
        sPlayer->play("qrc:///sounds/Warning.ogg");
259
269
        QMessageBox::critical(this,tr("FiSysMg - Message"),
260
270
                              tr("Unable to open fstab file!"));
261
271
        return;
262
272
    }
263
273
    if (! saveFile.open(QIODevice::WriteOnly|QIODevice::Truncate|QIODevice::Text)) {
264
 
        sPlayer->play("qrc:///sounds/Warning.mp3");
 
274
        sPlayer->play("qrc:///sounds/Warning.ogg");
265
275
        QMessageBox::critical(this,tr("FiSysMg - Message"),
266
276
                              tr("Unable to create backup file!"));
267
277
        fstabFile.close();
268
278
        return;
269
279
    }
270
280
    if (saveFile.write(fstabHash.toLocal8Bit().data()) != fstabHash.length()) {
271
 
        sPlayer->play("qrc:///sounds/Warning.mp3");
 
281
        sPlayer->play("qrc:///sounds/Warning.ogg");
272
282
        QMessageBox::critical(this,tr("FiSysMg - Message"),
273
283
                              tr("Unable to write to backup file!"));
274
284
        saveFile.close();
276
286
        return;
277
287
    }
278
288
    if (saveFile.write("\n") != 1) {
279
 
        sPlayer->play("qrc:///sounds/Warning.mp3");
 
289
        sPlayer->play("qrc:///sounds/Warning.ogg");
280
290
        QMessageBox::critical(this,tr("FiSysMg - Message"),
281
291
                              tr("Unable to write to backup file!"));
282
292
        saveFile.close();
286
296
    while (! fstabFile.atEnd()) {
287
297
        inLine =  fstabFile.readLine();
288
298
        if (inLine == "") {
289
 
            sPlayer->play("qrc:///sounds/Warning.mp3");
 
299
            sPlayer->play("qrc:///sounds/Warning.ogg");
290
300
            QMessageBox::critical(this,tr("FiSysMg - Message"),
291
301
                                  tr("Unable to read fstab file!"));
292
302
            saveFile.close();
294
304
            return;
295
305
        }
296
306
        if (saveFile.write(inLine.toLocal8Bit().data()) != inLine.length()) {
297
 
            sPlayer->play("qrc:///sounds/Warning.mp3");
 
307
            sPlayer->play("qrc:///sounds/Warning.ogg");
298
308
            QMessageBox::critical(this,tr("FiSysMg - Message"),
299
309
                                  tr("Unable to write to backup file!"));
300
310
            saveFile.close();
343
353
    fileName = fileDialog.selectedFiles()[0];
344
354
    saveFile.setFileName(fileName);
345
355
    if (! saveFile.open(QIODevice::ReadOnly|QIODevice::Text)) {
346
 
        sPlayer->play("qrc:///sounds/Warning.mp3");
 
356
        sPlayer->play("qrc:///sounds/Warning.ogg");
347
357
        QMessageBox::critical(this,tr("FiSysMg - Message"),
348
358
                              tr("Unable to open backup file!"));
349
359
        return;
350
360
    }
351
361
    ws = saveFile.readLine();
352
362
    if (ws == "") {
353
 
        sPlayer->play("qrc:///sounds/Warning.mp3");
 
363
        sPlayer->play("qrc:///sounds/Warning.ogg");
354
364
        QMessageBox::critical(this,tr("FiSysMg - Message"),
355
365
                              tr("Unable to read backup file!"));
356
366
        saveFile.close();
366
376
    while (! saveFile.atEnd()) {
367
377
        ws = saveFile.readLine();
368
378
        if (ws == "") {
369
 
            sPlayer->play("qrc:///sounds/Warning.mp3");
 
379
            sPlayer->play("qrc:///sounds/Warning.ogg");
370
380
            QMessageBox::critical(this,tr("FiSysMg - Message"),
371
381
                                  tr("Unable to read backup file!"));
372
382
            saveFile.close();
376
386
    }
377
387
    saveFile.close();
378
388
    if (helperProc->write(accessKey.toLocal8Bit().data()) != accessKey.length()) {
379
 
        sPlayer->play("qrc:///sounds/Warning.mp3");
 
389
        sPlayer->play("qrc:///sounds/Warning.ogg");
380
390
        QMessageBox::critical(this,tr("FiSysMg - Message"),
381
391
                              tr("Unable to write to helper process!"));
382
392
        return;
383
393
    }
384
394
    if (helperProc->write(":copy\n") != 6) {
385
 
        sPlayer->play("qrc:///sounds/Warning.mp3");
 
395
        sPlayer->play("qrc:///sounds/Warning.ogg");
386
396
        QMessageBox::critical(this,tr("FiSysMg - Message"),
387
397
                              tr("Unable to write 'copy' to helper process!"));
388
398
        return;
390
400
    lineCount = fileList.count();
391
401
    for (int idy = 0; idy < lineCount; idy++) {
392
402
        if (helperProc->write(fileList[idy].toLocal8Bit().data()) != fileList[idy].length()) {
393
 
            sPlayer->play("qrc:///sounds/Warning.mp3");
 
403
            sPlayer->play("qrc:///sounds/Warning.ogg");
394
404
            QMessageBox::critical(this,tr("FiSysMg - Message"),
395
405
                                  tr("Unable to write file to helper process!"));
396
406
            return;
397
407
        }
398
408
    }
399
409
    if (helperProc->write("EOF\n") != 4) {
400
 
        sPlayer->play("qrc:///sounds/Warning.mp3");
 
410
        sPlayer->play("qrc:///sounds/Warning.ogg");
401
411
        QMessageBox::critical(this,tr("FiSysMg - Message"),
402
412
                              tr("Unable to write 'EOF' to helper process!"));
403
413
        return;
421
431
    if (ws == "success") {
422
432
        fstabModel->removeRows(0,fstabModel->rowCount());
423
433
        if (! loadfstab()) {
424
 
            sPlayer->play("qrc:///sounds/Warning.mp3");
 
434
            sPlayer->play("qrc:///sounds/Warning.ogg");
425
435
            QMessageBox::warning(this,tr("FiSysMg - Message"), tr("fstab restored but unable to reload it."));
426
436
           close();
427
437
           return;
428
438
        }
429
439
        QMessageBox::information(this,tr("FiSysMg - Message"), tr("fstab restored successfully."));
430
440
    } else {
431
 
        sPlayer->play("qrc:///sounds/Warning.mp3");
 
441
        sPlayer->play("qrc:///sounds/Warning.ogg");
432
442
        QMessageBox::warning(this,tr("FiSysMg - Message"), tr("Unable to restore fstab."));
433
443
    }
434
444
}
609
619
    QString             outLine;
610
620
 
611
621
    if (helperProc->write(accessKey.toLocal8Bit().data()) != accessKey.length()) {
612
 
        sPlayer->play("qrc:///sounds/Warning.mp3");
 
622
        sPlayer->play("qrc:///sounds/Warning.ogg");
613
623
        QMessageBox::critical(this,tr("FiSysMg - Message"),
614
624
                              tr("Unable to write to helper process!"));
615
625
        return;
616
626
    }
617
627
    if (helperProc->write(":copy\n") != 6) {
618
 
        sPlayer->play("qrc:///sounds/Warning.mp3");
 
628
        sPlayer->play("qrc:///sounds/Warning.ogg");
619
629
        QMessageBox::critical(this,tr("FiSysMg - Message"),
620
630
                              tr("Unable to write 'copy' to helper process!"));
621
631
        return;
642
652
        }
643
653
        outLine += "\n";
644
654
        if (helperProc->write(outLine.toLocal8Bit().data()) != outLine.length()) {
645
 
            sPlayer->play("qrc:///sounds/Warning.mp3");
 
655
            sPlayer->play("qrc:///sounds/Warning.ogg");
646
656
            QMessageBox::critical(this,tr("FiSysMg - Message"),
647
657
                                  tr("Unable to write to helper process!"));
648
658
            return;
649
659
        }
650
660
    }
651
661
    if( helperProc->write("EOF\n") != 4) {
652
 
        sPlayer->play("qrc:///sounds/Warning.mp3");
 
662
        sPlayer->play("qrc:///sounds/Warning.ogg");
653
663
        QMessageBox::critical(this,tr("FiSysMg - Message"),
654
664
                              tr("Unable to write 'EOF' to helper process!"));
655
665
        return;
673
683
    if (ws == "success") {
674
684
        QMessageBox::information(this,tr("FiSysMg - Message"), tr("Changes saved successfully."));
675
685
    } else {
676
 
        sPlayer->play("qrc:///sounds/Warning.mp3");
 
686
        sPlayer->play("qrc:///sounds/Warning.ogg");
677
687
        QMessageBox::warning(this,tr("FiSysMg - Message"), tr("Unable to save changes."));
678
688
    }
679
689
}
696
706
    filterProxy.setData(CurItemIdx, NextRow);
697
707
    filterProxy.setData(NextItemIdx, CurRow);
698
708
}
 
709
 
 
710
void FsmMain::on_action_Manage_Credentials_triggered() {
 
711
 
 
712
    CredsDialog credsDialog(1, this);
 
713
 
 
714
    winWidth = settings->value("CredsDialog/Width", 300).toInt();
 
715
    winHeight = settings->value("CredsDialog/Height", 200).toInt();
 
716
    credsDialog.resize(winWidth, winHeight);
 
717
    credsDialog.setValue(curCreds);
 
718
    credsDialog.exec();
 
719
    if ((winWidth != credsDialog.width()) || (winHeight != credsDialog.height())) {
 
720
        winWidth = credsDialog.width();
 
721
        winHeight = credsDialog.height();
 
722
        if (winWidth < 300)
 
723
            winWidth = 300;
 
724
        if (winHeight < 210)
 
725
            winHeight = 210;
 
726
        settings->setValue("CredsDialog/Width", winWidth);
 
727
        settings->setValue("CredsDialog/Height", winHeight);
 
728
    }
 
729
}