~ben-kietzman/ubuntu/quantal/mountmanager/fix-for-598070

« back to all changes in this revision

Viewing changes to plugins/ImagesMounting/imagesmounting.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Fabrice Coutadeur
  • Date: 2008-08-20 10:22:14 UTC
  • Revision ID: james.westby@ubuntu.com-20080820102214-fv93myu0ncb1503r
Tags: upstream-0.2.4
ImportĀ upstreamĀ versionĀ 0.2.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//MountManager - the program for easy mounting of storage devices in Linux
 
2
//Copyright (C) 2007-2008 Tikhonov Sergey
 
3
//
 
4
//This file is part of MountManager Plugin "Images mounting"
 
5
//
 
6
//This program is free software; you can redistribute it and/or
 
7
//modify it under the terms of the GNU General Public License
 
8
//as published by the Free Software Foundation; either version 2
 
9
//of the License, or (at your option) any later version.
 
10
//
 
11
//This program is distributed in the hope that it will be useful,
 
12
//but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
//GNU General Public License for more details.
 
15
//
 
16
//You should have received a copy of the GNU General Public License
 
17
//along with this program; if not, write to the Free Software
 
18
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
19
#include <QtGui/QDialog>
 
20
#include <QtGui/QAction>
 
21
#include <QtGui/QGridLayout>
 
22
#include <QtGui/QPushButton>
 
23
#include <QtGui/QComboBox>
 
24
#include <QtGui/QLineEdit>
 
25
#include <QtGui/QLabel>
 
26
#include <QtGui/QCheckBox>
 
27
#include <QtGui/QFileDialog>
 
28
#include <QtCore/QDir>
 
29
#include <QtCore/QUrl>
 
30
#include <QtPlugin>
 
31
#include <QDesktopServices>
 
32
#include "imagesmounting.h"
 
33
 
 
34
ImagesMounting::ImagesMounting(QWidget *parent) {
 
35
        mainWidget = new QDialog(parent);
 
36
 
 
37
        // To define open or not mount point directory after mounting
 
38
        wasMounting = false;
 
39
 
 
40
        homePath = QDir::homePath();
 
41
        if (!QDir(homePath + "/.mountmanager/").exists())
 
42
                QDir().mkpath(homePath + "/.mountmanager/");
 
43
        tempIsoImagePath = homePath + "/.mountmanager/temp_isoimage.iso";
 
44
        
 
45
        process = new QProcess(this);
 
46
        connect(process,SIGNAL(finished(int,QProcess::ExitStatus)),this,SLOT(processFinished(int,QProcess::ExitStatus)));
 
47
        
 
48
        QAction *action = new QAction(this);
 
49
        action->setText(tr("Images mounting"));
 
50
        action->setIcon(QIcon(":/icons/main.png"));
 
51
        connect(action,SIGNAL(triggered()),mainWidget,SLOT(show()));
 
52
        actions.append(action);
 
53
        
 
54
        mountButton = new QPushButton(tr("Mount"));
 
55
        connect(mountButton,SIGNAL(clicked()),this,SLOT(mount()));
 
56
        
 
57
        unmountButton = new QPushButton(tr("Unmount"));
 
58
        connect(unmountButton,SIGNAL(clicked()),this,SLOT(unmount()));
 
59
        
 
60
        closeButton = new QPushButton(tr("Close"));
 
61
        connect(closeButton,SIGNAL(clicked()),mainWidget,SLOT(hide()));
 
62
 
 
63
        imagePath = new QLineEdit;
 
64
        mountPointPath = new QLineEdit;
 
65
        chooseMountPointPath = new QPushButton;
 
66
        chooseMountPointPath->setIcon(QIcon(":/icons/choose.png"));
 
67
        connect(chooseMountPointPath,SIGNAL(clicked()),this,SLOT(chooseMountPoint()));
 
68
        
 
69
        chooseImagePath = new QPushButton;
 
70
        chooseImagePath->setIcon(QIcon(":/icons/choose.png"));
 
71
        connect(chooseImagePath,SIGNAL(clicked()),this,SLOT(chooseImage()));
 
72
 
 
73
        openDirAfterMounting = new QCheckBox(tr("Open directory after mounting"));
 
74
 
 
75
        QHBoxLayout *buttonsLayout = new QHBoxLayout;
 
76
        buttonsLayout->addStretch();
 
77
        buttonsLayout->addWidget(mountButton);
 
78
        buttonsLayout->addWidget(unmountButton);
 
79
        buttonsLayout->addWidget(closeButton);
 
80
 
 
81
        QGridLayout *gridLayout = new QGridLayout;
 
82
        gridLayout->addWidget(new QLabel(tr("Choose image") + ":"),0,0);
 
83
        gridLayout->addWidget(imagePath,0,1);
 
84
        gridLayout->addWidget(chooseImagePath,0,2);
 
85
        gridLayout->addWidget(new QLabel(tr("Choose mount point") + ":"),1,0);
 
86
        gridLayout->addWidget(mountPointPath,1,1);
 
87
        gridLayout->addWidget(chooseMountPointPath,1,2);
 
88
        gridLayout->setColumnStretch(1,1);
 
89
        
 
90
        QVBoxLayout *mainLayout = new QVBoxLayout;
 
91
        mainLayout->addLayout(gridLayout);
 
92
        mainLayout->addWidget(openDirAfterMounting);
 
93
        mainLayout->addStretch();
 
94
        mainLayout->addLayout(buttonsLayout);
 
95
        mainWidget->setLayout(mainLayout);
 
96
        mainWidget->setWindowTitle(tr("Images mounting"));
 
97
        mainWidget->setWindowIcon(QIcon(":/icons/main.png"));
 
98
        mainWidget->resize(500,mainWidget->sizeHint().height());
 
99
}
 
100
 
 
101
ImagesMounting::~ImagesMounting() {
 
102
        delete mainWidget;
 
103
        foreach (QAction *action,actions)
 
104
                delete action;
 
105
        delete mountButton;
 
106
        delete unmountButton;
 
107
        delete closeButton;
 
108
        delete imagePath;
 
109
        delete mountPointPath;
 
110
        delete chooseImagePath;
 
111
        delete chooseMountPointPath;
 
112
        delete openDirAfterMounting;
 
113
}
 
114
 
 
115
void ImagesMounting::setParent(QObject *parent) {
 
116
        connect(this,SIGNAL(showError(const QString&)),parent,SLOT(showError(const QString &)));
 
117
}
 
118
 
 
119
const QString ImagesMounting::pluginName() const {
 
120
        return tr("Images mounting");
 
121
}
 
122
 
 
123
const QString ImagesMounting::pluginDescription() const {
 
124
        return tr("With help of this plugin you can mount images of different formats from Nero to Iso");
 
125
}
 
126
        
 
127
QDialog* ImagesMounting::dialog() const {
 
128
        return mainWidget;
 
129
}
 
130
 
 
131
QDockWidget* ImagesMounting::dockWidget() const {
 
132
        return 0;
 
133
}
 
134
 
 
135
QMenu* ImagesMounting::menu() const {
 
136
        return 0;
 
137
}
 
138
 
 
139
QToolBar* ImagesMounting::toolBar() const {
 
140
        return 0;
 
141
}
 
142
 
 
143
Actions ImagesMounting::menuActions() const {
 
144
        return actions;
 
145
}
 
146
 
 
147
void ImagesMounting::chooseImage() {
 
148
        QString path = QFileDialog::getOpenFileName(mainWidget,tr("Choose image"),homePath,tr("Images formats (*.iso *.nrg *.bin *.mdf *.ccd *.img *.sub *.b5i *.cdi *.pdi *.dmg)"));
 
149
        if (path.isEmpty())
 
150
                return;
 
151
        imageType = QFileInfo(path).fileName().split(".").last();
 
152
        imagePath->setText(path);
 
153
}
 
154
 
 
155
void ImagesMounting::chooseMountPoint() {
 
156
        QString path = QFileDialog::getExistingDirectory(mainWidget,tr("Choose mount point"),"/mnt");
 
157
        if (path.isEmpty())
 
158
                return;
 
159
        mountPointPath->setText(path);
 
160
}
 
161
 
 
162
void ImagesMounting::mount() {
 
163
        if (imagePath->text().isEmpty()) {
 
164
                imagePath->setFocus();
 
165
                return;
 
166
        }
 
167
        if (mountPointPath->text().isEmpty()) {
 
168
                mountPointPath->setFocus();
 
169
                return;
 
170
        }
 
171
        if (imageType != "iso" && convert()) {
 
172
                wasMounting = true;
 
173
                process->start(QString("mount -t iso9660 -o loop \"%1\" \"%2\"").arg(tempIsoImagePath).arg(mountPointPath->text()));
 
174
        }
 
175
        else if (imageType == "iso") {
 
176
                process->start(QString("mount -t iso9660 -o loop \"%1\" \"%2\"").arg(imagePath->text()).arg(mountPointPath->text()));
 
177
                wasMounting = true;
 
178
        }
 
179
}
 
180
 
 
181
bool ImagesMounting::convert() {
 
182
        process->waitForFinished();
 
183
        QString program;
 
184
        // Define the program for converting to iso
 
185
        if (imageType.isEmpty() && QFileInfo(imagePath->text()).fileName().split(".").count() > 1)
 
186
                imageType = QFileInfo(imagePath->text()).fileName().split(".").last();
 
187
        if (imageType.isEmpty()) {
 
188
                emit(showError(tr("[%1] File name is invalid.").arg(pluginName())));
 
189
                return false;
 
190
        }
 
191
        if (imageType == "nrg")
 
192
                program = "nrg2iso";
 
193
        else if (imageType == "bin" || imageType == "cue")
 
194
                program = "bchunk";
 
195
        else if (imageType == "mdf")
 
196
                program = "mdf2iso";
 
197
        else if (imageType == "img" || imageType == "ccd" || imageType == "sub")
 
198
                program = "ccd2iso";
 
199
        else if (imageType == "b5i")
 
200
                program = "b5i2iso";
 
201
        else if (imageType == "cdi")
 
202
                program = "cdi2iso";
 
203
        else if (imageType == "pdi")
 
204
                program = "pdi2iso";
 
205
        else if (imageType == "dmg")
 
206
                program = "dmg2iso";
 
207
        if (program.isEmpty())
 
208
                return false;
 
209
        process->start(QString("%1 \"%2\" \"%3\"").arg(program).arg(imagePath->text()).arg(tempIsoImagePath));
 
210
        if (process->readAllStandardError().isEmpty())
 
211
                return true;
 
212
        emit(showError(tr("[%1] Cannot to convert your image to iso image. Maybe image is corrupted").arg(pluginName())));
 
213
        return false;
 
214
}
 
215
 
 
216
void ImagesMounting::processFinished(int exitCode,QProcess::ExitStatus) {
 
217
        if (exitCode != 0)
 
218
                emit(showError(tr("[%1] We are sorry, but was error").arg(pluginName()) + ": " + QString::fromUtf8(process->readAllStandardError().data())));
 
219
        else if (wasMounting && openDirAfterMounting->isChecked())
 
220
                QDesktopServices::openUrl(QUrl(mountPointPath->text()));
 
221
        if (exitCode == 0 && wasMounting) {
 
222
                imagePath->clear();
 
223
                mountPointPath->clear();
 
224
        }
 
225
        wasMounting = false;
 
226
}
 
227
 
 
228
void ImagesMounting::unmount() {
 
229
        QString path = QFileDialog::getExistingDirectory(mainWidget,tr("Choose mount point"),"/mnt");
 
230
        if (path.isEmpty())
 
231
                return;
 
232
        process->start(QString("umount %1").arg(path));
 
233
}
 
234
 
 
235
Q_EXPORT_PLUGIN2(imagesmounting,ImagesMounting)