UBUpdateDlg.cpp 5.44 KB
Newer Older
Claudio Valerio's avatar
Claudio Valerio committed
1
/*
2
 * Copyright (C) 2012 Webdoc SA
Claudio Valerio's avatar
Claudio Valerio committed
3
 *
4 5 6 7 8 9 10 11 12 13
 * This file is part of Open-Sankoré.
 *
 * Open-Sankoré is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation, version 2,
 * with a specific linking exception for the OpenSSL project's
 * "OpenSSL" library (or with modified versions of it that use the
 * same license as the "OpenSSL" library).
 *
 * Open-Sankoré is distributed in the hope that it will be useful,
Claudio Valerio's avatar
Claudio Valerio committed
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
Claudio Valerio's avatar
Claudio Valerio committed
17
 *
18 19 20
 * You should have received a copy of the GNU Library General Public
 * License along with Open-Sankoré; if not, see
 * <http://www.gnu.org/licenses/>.
Claudio Valerio's avatar
Claudio Valerio committed
21
 */
22 23


24 25 26
#include <QFileDialog>

#include "UBUpdateDlg.h"
27 28
#include "core/UBApplication.h"
#include "UBMainWindow.h"
29

30 31
#include "core/memcheck.h"

32
UBUpdateDlg::UBUpdateDlg(QWidget *parent, int nbFiles, const QString& bkpPath)
33 34 35 36 37 38 39 40 41 42 43 44 45 46
    : QDialog(parent)
    , mMainLayout(NULL)
    , mNbFilesLabel(NULL)
    , mBkpLabel(NULL)
    , mBkpPath(NULL)
    , mBrowseBttn(NULL)
    , mpDlgBttn(NULL)
    , mLayout(NULL)
    , mHLayout(NULL)
    , mStackedWidget(NULL)
    , mDialogWidget(NULL)
    , mProgressWidget(NULL)
    , mProgressLayout(NULL)
    , mProgressLabel(NULL)
47

48
{
49 50 51 52 53 54 55 56
    mDialogWidget = new QWidget(this);
    mProgressWidget = new QWidget(this);

    mStackedWidget = new QStackedWidget(this);
    mStackedWidget->addWidget(mDialogWidget);
    mStackedWidget->addWidget(mProgressWidget);

    setFixedSize(450, 110);
57 58
    setModal(true);
    setWindowTitle(tr("Document updater"));
59 60 61
    mLayout = new QVBoxLayout();
    setLayout(mLayout);

62 63
    QString str = QString::number(nbFiles);
    str.append(tr(" files require an update."));
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
    mNbFilesLabel = new QLabel(mDialogWidget);
    mNbFilesLabel->setText(str);

    mLayout->addWidget(mNbFilesLabel);

    mBkpLabel = new QLabel(mDialogWidget);
    mBkpLabel->setText(tr("Backup path: "));

    mBkpPath = new QLineEdit(mDialogWidget);
    mBkpPath->setText(bkpPath);

    mBrowseBttn = new QPushButton(mDialogWidget);
    mBrowseBttn->setText(tr("Browse"));

    mHLayout = new QHBoxLayout();
    mHLayout->addWidget(mBkpLabel);
    mHLayout->addWidget(mBkpPath, 1);
    mHLayout->addWidget(mBrowseBttn);

    mLayout->addLayout(mHLayout);

    mpDlgBttn = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, mDialogWidget);
    mLayout->addWidget(mpDlgBttn);
87 88

    mpDlgBttn->button(QDialogButtonBox::Ok)->setText(tr("Update"));
89
    mpDlgBttn->button(QDialogButtonBox::Cancel)->setText(tr("Remind me later"));
90

91
    QObject::connect(mBrowseBttn, SIGNAL(clicked()), this, SLOT(onBrowse()));
92 93
    QObject::connect(mpDlgBttn, SIGNAL(accepted()), this, SLOT(onUpdate()));
    QObject::connect(mpDlgBttn, SIGNAL(rejected()), this, SLOT(reject()));
94 95 96 97 98 99
    mDialogWidget->setLayout(mLayout);
    mStackedWidget->setCurrentWidget(mDialogWidget);

    mMainLayout = new QVBoxLayout();
    this->setLayout(mMainLayout);
    mMainLayout->addWidget(mStackedWidget);
100 101 102 103
}

UBUpdateDlg::~UBUpdateDlg()
{
104
    if (NULL != mpDlgBttn)
105 106 107 108
    {
        delete mpDlgBttn;
        mpDlgBttn = NULL;
    }
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

    if (mNbFilesLabel) {
        delete mNbFilesLabel;
        mNbFilesLabel = NULL;
    }

    if (mBkpLabel) {
        delete mBkpLabel;
        mBkpLabel = NULL;
    }

    if (mBkpPath) {
        delete mBkpPath;
        mBkpPath = NULL;
    }

    if (mBrowseBttn) {
        delete mBrowseBttn;
        mBrowseBttn = NULL;
    }

    if (mProgressLabel) {
        delete mProgressLabel;
        mProgressLabel = NULL;
    }

    if (mHLayout) {
        delete mHLayout;
        mHLayout = NULL;
    }

    if (mLayout) {
        delete mLayout;
        mLayout = NULL;
    }

    if (mProgressLayout) {
        delete mProgressLayout;
        mProgressLayout = NULL;
    }

    if (mDialogWidget) {
        delete mDialogWidget;
        mDialogWidget = NULL;
    }

    if (mProgressWidget) {
        delete mProgressWidget;
        mProgressWidget = NULL;
    }

    if (mStackedWidget) {
        delete mStackedWidget;
        mStackedWidget = NULL;
    }

    if (mMainLayout) {
        delete mMainLayout;
        mMainLayout = NULL;
    }
169 170 171 172 173
}

void UBUpdateDlg::onBrowse()
{
    QString qsSelectedDir;
174 175
    qsSelectedDir = QFileDialog::getExistingDirectory(this, tr("Select a backup folder"), mBkpPath->text());
    mBkpPath->setText(qsSelectedDir);
176 177 178 179
}

void UBUpdateDlg::onUpdate()
{
180
    mProgressLabel = new QLabel(mProgressWidget);
181
    mProgressLabel->setText(tr("Please wait the import process will start soon..."));
182 183 184 185
    mProgressLayout = new QHBoxLayout();
    mProgressLayout->addWidget(mProgressLabel);
    mProgressWidget->setLayout(mProgressLayout);
    mStackedWidget->setCurrentWidget(mProgressWidget);
186 187 188 189 190
    emit updateFiles();
}

void UBUpdateDlg::onFilesUpdated(bool bResult)
{
191
    this->hide();
192 193
    QString qsMsg;

194
    if (bResult) {
195
        qsMsg = tr("Files update successful!\nPlease reboot the application to access the updated documents.");
196
    }
197
    else {
198 199
        qsMsg = tr("An error occured during the update. The files have not been affected.");
    }
200
    UBApplication::mainWindow->information(tr("Files update results"), qsMsg);
201 202 203 204
}

QString UBUpdateDlg::backupPath()
{
205
    return mBkpPath->text();
206
}
207 208 209 210 211 212

void UBUpdateDlg::transitioningFile(QString fileName)
{
    mProgressLabel->setText(tr("Updating file ") + fileName);
}