~bedouin/+junk/x-gin

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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDir>
#include <QFile>
#include <QDebug>
#include <QTextStream>
#include <QMessageBox>
#include <QStringList>
#include <QDebug>
#include <QFileDialog>
#include <QTranslator>
#include <QLocale>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    /**
     * @brief bahasa
     * Changing UI language to Bahasa indonesia
     */
    QString bahasa = QLocale::system().name();          // e.g. "id_ID"
    bahasa.truncate(bahasa.lastIndexOf('_'));           // e.g. "id"

    QTranslator interpret;
    if(bahasa == "id")
    {
        interpret.load("xgin_"+bahasa,QDir::currentPath()+"/lang");
        qApp->installTranslator(&interpret);
        ui->retranslateUi(this);
    }

#if(Q_OS_WIN32)
    ui->stackedWidget->setCurrentIndex(4);
    ui->mainToolBar->hide();
#endif

    /**
     * Constructor of the helper dialog.
     * It is used for displaying about dialog
     * and app preferences.
     */
    helper = new Dialog(this);
    helper->setModal(true);
    helper->setVersion(QT_VERSION_STR);

    ui->splitter_2->setStretchFactor(0,1);
    ui->splitter_2->setStretchFactor(1,0);
    ui->btnCloseInfoFile->setVisible(false);
    ui->textInfoFile->setVisible(false);
    ui->configText->setVisible(false);
    //ui->treeFiles->resizeColumnToContents(0);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
    ui->treeFiles->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
#else
    ui->treeFiles->header()->setResizeMode(QHeaderView::ResizeToContents);
#endif

    /**
     * @brief nginxRoot
     * It is the root directory of nginx-portable.
     */
    QDir nginxRoot(helper->nginxDir());

    /**
     * @brief nginxDir
     * Don't be confused with `nginxDir` from
     * the helper dialog.
     */
    QString nginxPath;

    /**
     * Checking whether nginxRoot exists.
     */
    if(nginxRoot.exists())
    {
        nginxPath = nginxRoot.absolutePath();
        //qDebug() << nginxRoot.exists();
    }
    else
    {
        helper->selectPage(1);
        helper->exec();
    }

    /**
     * Checking if nginxDir is not empty
     */
    if(!nginxPath.isEmpty())
    {
        /**
         * Create new QFileSystemModel for viewing log files
         */
        logs = new QFileSystemModel;

        /**
         * Create new QProcess variables.
         */
        strNginx = new QProcess(this);
        pidNginx = new QProcess(this);
        connect(strNginx,SIGNAL(finished(int)),this,SLOT(startNginx()));
        connect(pidNginx,SIGNAL(finished(int)),this,SLOT(pidofNginx()));

        /**
         * @brief lineServer
         * It is a line edit widget storing the server name of Ubuntu repository
         * TODO:
         * create a function to read and parse the file
         */
        connect(ui->lineServer,SIGNAL(textChanged(QString)),this,SLOT(toggleSave()));

        /**
         * Calling buildWidget();
         */
        this->buildWidget(nginxPath);

        /**
         * Provide file name for nginexec
         */
        nginexec.setFileName(nginxPath+"/nginx-portable");
        if(!nginexec.exists())
        {
            helper->selectPage(1);
            helper->exec();
        }
        //qDebug() << nginexec.exists();
    }
    else
    {
        ui->startButton->setEnabled(false);
        ui->statusBar->showMessage(tr("NginX Portable is not found. Set it from menu App -> Settings."));
    }

    /**
     * Push the items for QList of QActions
     */
    menu << ui->actionWorker << ui->actionFiles << ui->actionConfigure << ui->actionLogs;

    // TODO:
    // log line numbering (*)
    // action toggle (v)
    // file context menu (V)
    // baca file repo.lists => ekstrak server address (v)
    // edit /etc/hosts --> in the nginx-portable run script
    // settings run as root (v)
    // menempatkan file dalam folder masing2
    // jangan lupa mengubah hak akses folder & file

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_actionAbout_Qt_triggered()
{
    qApp->aboutQt();
}

void MainWindow::buildWidget(QString dir)
{
    /**
     * @brief model
     * Creating model for tree view
     */
    modelFiles = new QFileSystemModel;
    QModelIndex inds = modelFiles->setRootPath(dir+"/html");
    ui->treeFiles->setModel(modelFiles);
    ui->treeFiles->setRootIndex(inds);
    ui->treeFiles->setContextMenuPolicy(Qt::ActionsContextMenu);
    ui->treeFiles->addAction(ui->actionProperties);
    ui->treeFiles->addAction(ui->actionDelete);

    //qDebug() << QDir::currentPath();

    /**
     * @brief indx
     * Creating model index for the list view of log files
     */
    QModelIndex indx = logs->setRootPath(dir+"/logs");
    ui->listLogs->setModel(logs);
    ui->listLogs->setRootIndex(indx);
    ui->listLogs->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->listLogs->setFocus();

    //qDebug() << indx;
    /**
     * @brief logs
     * Connecting model of list view for log files to the showLog slot
     */
    connect(logs,SIGNAL(directoryLoaded(QString)),this,SLOT(showLog()));

    /**
     * @brief config
     * Setting file name for config, QFile
     * and displaying it to configText, QPlaintext
     */
    config.setFileName(dir+"/conf/nginx.conf");
    config.open(QIODevice::ReadWrite|QIODevice::Text);
    QTextStream confFile(&config);
    ui->configText->setPlainText(confFile.readAll());
    config.close();

    /**
     * @brief sourcesList
     * Read Ubuntu sources.list file to find out the current
     * repository server address
     * then set it to the configure tab in the lineEdit
     */
    QFile sourcesList("/etc/apt/sources.list");
    sourcesList.open(QIODevice::ReadOnly|QIODevice::Text);
    QTextStream sources(&sourcesList);
    while(!sources.atEnd())
    {
        QString lines = sources.readLine();
        if(lines.startsWith("deb "))
        {
            //qDebug() << lines;
            int startSlash = lines.indexOf("/")+2;
            int endSlash = lines.indexOf("/",startSlash);
            QString server = lines.mid(startSlash,endSlash-startSlash);
            //QStringList splits = lines.split(" ");
            //QString server = splits.at(1);
            ui->lineServer->setText(server);
            break;
        }
    }
    sourcesList.close();

}

void MainWindow::on_startButton_clicked(bool checked)
{
    if(checked)
    {
        /**
         * check if it needs root access
         */
        if(helper->asRoot())
        {
            strNginx->start("pkexec",QStringList() << "sh" << nginexec.fileName() << "start");
        }
        else
        {
            strNginx->start("sh",QStringList() << nginexec.fileName() << "start");
        }
        /**
         * TODO:
         * set the label below startButton to a GREEN image
         */
    }
    else
    {
        if(helper->asRoot())
        {
            strNginx->start("pkexec",QStringList() << "sh" << nginexec.fileName() << "stop");
        }
        else
        {
            strNginx->start("sh",QStringList() << nginexec.fileName() << "stop");
        }
        /**
         * TODO:
         * set the label below startButton to a RED image
         */
    }
}

void MainWindow::on_actionQuit_triggered()
{
    qApp->quit();
}

void MainWindow::on_actionAbout_triggered()
{
    helper->selectPage(0);
    helper->show();
}

void MainWindow::startNginx()
{
    pidNginx->start("pgrep nginx");
    //qDebug() << strNginx->exitCode();
    //strNginx->exitCode() => jika return 126 atau 127 berarti otentikasi gagal
    /**
     * Make sure if nginx is running by checking the
     * existence of its PID
     * TODO:
     * maybe it is useful if checking strNginx exitCode.
     * 126 or 127 means error
     */
}

void MainWindow::pidofNginx()
{
    QPixmap green(":/imgs/images/green.png");
    QPixmap red(":/imgs/images/red.png");
    //ui->nginxStatus->setText("...");
    QString pid = pidNginx->readAllStandardOutput();
    if(!pid.isEmpty())
    {
        ui->startButton->setText(tr("Started"));
        //qDebug() << strNginx->exitStatus();
        ui->statusBar->showMessage(tr("Nginx is running"));
        ui->nginxStatus->setPixmap(green);
    }
    else
    {
        //qDebug() << "Gagal/Failed";
        ui->startButton->setText(tr("Start"));
        ui->statusBar->showMessage(tr("Nginx has stopped"));
        ui->startButton->setChecked(false);
        ui->nginxStatus->setPixmap(red);
    }
    //qDebug() << pid;
}

void MainWindow::on_actionReport_Problem_triggered()
{
    /**
     * Show report dialog
     */
    helper->show();
}

void MainWindow::on_stackedWidget_currentChanged(int ind)
{
    //qDebug() << menu.at(ind);
    for(int i = 0; i<menu.size();++i)
    {
        menu.at(i)->setChecked(false);
    }

    /**
     * Set QAction at given index checked
     */
    menu.at(ind)->setChecked(true);
}

void MainWindow::showLog()
{
    QModelIndex chosen;
    chosen = logs->index(1,0,ui->listLogs->rootIndex());    //rootIndex ini yang dicari-cari gak ketemu

    //ui->listLogs->setCurrentIndex(pilih);
    ui->listLogs->selectionModel()->select(chosen,QItemSelectionModel::Select);

    /**
     * Emitting signal to clicked list item slot
     */
    emit this->on_listLogs_clicked(chosen);
}

void MainWindow::toggleSave()
{
    ui->buttonSave->setDisabled(false);
}

void MainWindow::on_listLogs_clicked(const QModelIndex &inx)
{
    //qDebug() << inx;
    QString file = logs->fileInfo(inx).absoluteFilePath();

    /**
     * @brief laporan
     * @return
     * Reading log file at given index in the list view
     */
    QFile laporan(file);
    ui->lineLog->setText(file);
    laporan.open(QIODevice::ReadOnly|QIODevice::Text);

    /**
     * @brief catatan
     * Reading the content of log files and showing it to QPlaintextedit
     */
    QTextStream logFile(&laporan);
    ui->logsText->setPlainText(logFile.readAll());

    //qDebug() << ui->listLogs->selectionModel()->selectedIndexes().at(1);
}

void MainWindow::on_actionWorker_triggered(bool checked)
{
    /**
     * This scope and three below
     * are for toggling between four QActions
     * in the toolbar and App menu.
     * Also they prevent QAction from unchecked.
     */
    if(checked)
        ui->stackedWidget->setCurrentIndex(0);
    else
        ui->actionWorker->setChecked(true);
}

void MainWindow::on_actionFiles_triggered(bool checked)
{
    if(checked)
        ui->stackedWidget->setCurrentIndex(1);
    else
        ui->actionFiles->setChecked(true);
}

void MainWindow::on_actionConfigure_triggered(bool checked)
{
    if(checked)
        ui->stackedWidget->setCurrentIndex(2);
    else
        ui->actionConfigure->setChecked(true);
}

void MainWindow::on_actionLogs_triggered(bool checked)
{
    if(checked)
        ui->stackedWidget->setCurrentIndex(3);
    else
        ui->actionLogs->setChecked(true);
}

void MainWindow::on_actionSettings_triggered()
{
    helper->selectPage(1);
    helper->exec();
}

void MainWindow::on_buttonSave_clicked()
{
    /**
     * TODO:
     * Save the config file
     */
}

void MainWindow::on_treeFiles_entered(const QModelIndex &index)
{
    QString info = modelFiles->fileInfo(index).absoluteFilePath();
    ui->statusBar->showMessage(info);
}

void MainWindow::on_btnCloseInfoFile_clicked()
{
    ui->textInfoFile->setVisible(false);
    ui->btnCloseInfoFile->setVisible(false);
}

void MainWindow::on_actionProperties_triggered()
{
    if(ui->textInfoFile->isHidden())
    {
        ui->textInfoFile->setVisible(true);
        ui->btnCloseInfoFile->setVisible(true);
    }
//    qDebug() << modelFiles->permissions(ui->treeFiles->currentIndex());
    QFileInfo information(modelFiles->fileInfo(ui->treeFiles->currentIndex()));
    QString info;
    QString permission = (information.isReadable()) ? "r" : "-";
    permission += (information.isWritable()) ? "w" : "-";
    permission += (information.isExecutable()) ? "x" : "-";

    permission += (information.permission(QFile::ReadGroup)) ? "r" : "-";
    permission += (information.permission(QFile::WriteGroup)) ? "w" : "-";
    permission += (information.permission(QFile::ExeGroup)) ? "x" : "-";

    permission += (information.permission(QFile::ReadOther)) ? "r" : "-";
    permission += (information.permission(QFile::WriteOther)) ? "w" : "-";
    permission += (information.permission(QFile::ExeOther)) ? "x" : "-";

    QString readable = (information.permission(QFile::ReadOther))
            ? tr("can be read")
            : tr("cannot be read");

    QString writable = (information.permission(QFile::WriteOther))
            ? tr("is writable or editable.")
            : tr("is not writable or editable.");

    info = tr("The selected item %1 and %2").arg(readable).arg(writable);
    ui->textInfoFile->setPlainText(info+"\nPermission: "+permission);
}