2008-12-27 22:12:42 +00:00
|
|
|
/***************************************************************************
|
2010-01-06 16:15:13 +00:00
|
|
|
* Copyright (C) 2008, 2009, 2010 by Volker Lanz <vl@fidra.de> *
|
2008-12-27 22:12:42 +00:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the *
|
|
|
|
* Free Software Foundation, Inc., *
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#include "util/helpers.h"
|
|
|
|
#include "util/globallog.h"
|
|
|
|
|
2010-03-21 10:50:44 +00:00
|
|
|
#include "backend/corebackendmanager.h"
|
|
|
|
|
2008-12-27 22:12:42 +00:00
|
|
|
#include "ops/operation.h"
|
|
|
|
|
|
|
|
#include <kaboutdata.h>
|
2014-05-13 23:53:37 +01:00
|
|
|
#include <KLocalizedString>
|
2008-12-27 22:12:42 +00:00
|
|
|
#include <kmessagebox.h>
|
|
|
|
#include <kglobal.h>
|
|
|
|
#include <kcomponentdata.h>
|
2009-11-23 18:27:21 +00:00
|
|
|
#include <kstandarddirs.h>
|
|
|
|
#include <kcmdlineargs.h>
|
2010-03-07 19:02:02 +00:00
|
|
|
#include <kmenu.h>
|
2011-06-12 10:31:41 +01:00
|
|
|
#include <kstringhandler.h>
|
2009-11-23 18:27:21 +00:00
|
|
|
|
2010-05-21 19:09:55 +01:00
|
|
|
#include <solid/device.h>
|
|
|
|
|
2009-11-23 18:27:21 +00:00
|
|
|
#include <QProcess>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QApplication>
|
2010-02-17 19:39:24 +00:00
|
|
|
#include <QPainter>
|
|
|
|
#include <QIcon>
|
|
|
|
#include <QPixmap>
|
|
|
|
#include <QRect>
|
2010-03-07 19:02:02 +00:00
|
|
|
#include <QTreeWidget>
|
|
|
|
#include <QHeaderView>
|
2010-02-17 19:39:24 +00:00
|
|
|
|
|
|
|
#include <config.h>
|
2008-12-27 22:12:42 +00:00
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
void registerMetaTypes()
|
|
|
|
{
|
|
|
|
qRegisterMetaType<Operation*>("Operation*");
|
2010-01-28 16:22:31 +00:00
|
|
|
qRegisterMetaType<Log::Level>("Log::Level");
|
2008-12-27 22:12:42 +00:00
|
|
|
}
|
|
|
|
|
2009-11-23 18:27:21 +00:00
|
|
|
static QString suCommand()
|
|
|
|
{
|
2009-11-23 21:23:28 +00:00
|
|
|
KStandardDirs d;
|
|
|
|
const char* candidates[] = { "kdesu", "kdesudo", "gksudo", "gksu" };
|
|
|
|
QString rval;
|
2009-11-23 18:27:21 +00:00
|
|
|
|
2009-11-23 21:23:28 +00:00
|
|
|
for (quint32 i = 0; i < sizeof(candidates) / sizeof(candidates[0]); i++)
|
|
|
|
{
|
|
|
|
rval = d.locate("exe", candidates[i]);
|
|
|
|
if (QFileInfo(rval).isExecutable())
|
|
|
|
return rval;
|
|
|
|
}
|
2009-11-23 18:27:21 +00:00
|
|
|
|
2009-11-23 21:23:28 +00:00
|
|
|
return QString();
|
2009-11-23 18:27:21 +00:00
|
|
|
}
|
|
|
|
|
2008-12-27 22:12:42 +00:00
|
|
|
bool checkPermissions()
|
|
|
|
{
|
|
|
|
if (geteuid() != 0)
|
2009-11-23 18:27:21 +00:00
|
|
|
{
|
2010-05-21 18:12:44 +01:00
|
|
|
KCmdLineArgs* args = KCmdLineArgs::parsedArgs();
|
2010-02-17 19:39:24 +00:00
|
|
|
// only try to gain root privileges if we have a valid (kde|gk)su(do) command and
|
|
|
|
// we did not try so before: the dontsu-option is there to make sure there are no
|
2009-11-23 18:27:21 +00:00
|
|
|
// endless loops of calling the same non-working (kde|gk)su(do) binary again and again.
|
2010-05-21 18:12:44 +01:00
|
|
|
if (!suCommand().isEmpty() && !args->isSet("dontsu"))
|
2009-11-23 18:27:21 +00:00
|
|
|
{
|
2012-02-04 10:14:13 +00:00
|
|
|
QStringList argList;
|
|
|
|
|
|
|
|
const QString suCmd = suCommand();
|
2013-06-23 10:44:38 +01:00
|
|
|
|
2012-02-04 10:14:13 +00:00
|
|
|
// kdesu broke backward compatibility at some point and now only works with "-c";
|
|
|
|
// kdesudo accepts either (with or without "-c"), but the gk* helpers only work
|
|
|
|
// without. kdesu maintainers won't fix their app, so we need to work around that here.
|
2012-02-04 10:29:41 +00:00
|
|
|
if (suCmd.indexOf("kdesu") != -1)
|
2012-02-04 10:14:13 +00:00
|
|
|
argList << "-c";
|
|
|
|
|
|
|
|
argList << args->allArguments().join(" ") + " --dontsu";
|
|
|
|
|
|
|
|
if (QProcess::execute(suCmd, argList) == 0)
|
2009-11-23 18:27:21 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-06-23 10:44:38 +01:00
|
|
|
|
2008-12-27 22:12:42 +00:00
|
|
|
return KMessageBox::warningContinueCancel(NULL, i18nc("@info",
|
|
|
|
"<para><warning>You do not have administrative privileges.</warning></para>"
|
|
|
|
"<para>It is possible to run <application>%1</application> without these privileges. "
|
|
|
|
"You will, however, <emphasis>not</emphasis> be allowed to apply operations.</para>"
|
|
|
|
"<para>Do you want to continue running <application>%1</application>?</para>",
|
|
|
|
KGlobal::mainComponent().aboutData()->programName()),
|
|
|
|
i18nc("@title:window", "No administrative privileges"),
|
2010-04-11 19:19:24 +01:00
|
|
|
KGuiItem(i18nc("@action:button", "Run without administrative privileges"), "arrow-right"),
|
2008-12-27 22:12:42 +00:00
|
|
|
KStandardGuiItem::cancel(),
|
|
|
|
"runWithoutRootPrivileges") == KMessageBox::Continue;
|
2009-11-23 18:27:21 +00:00
|
|
|
}
|
2008-12-27 22:12:42 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
KAboutData* createPartitionManagerAboutData()
|
|
|
|
{
|
|
|
|
KAboutData* about = new KAboutData(
|
|
|
|
"partitionmanager",
|
|
|
|
NULL,
|
|
|
|
ki18nc("@title", "<application>KDE Partition Manager</application>"),
|
|
|
|
VERSION,
|
|
|
|
ki18nc("@title", "Manage your disks, partitions and file systems"),
|
|
|
|
KAboutData::License_GPL,
|
2011-08-28 19:35:50 +01:00
|
|
|
ki18nc("@info:credit", "(c) 2008, 2009, 2010, 2011 Volker Lanz")
|
2008-12-27 22:12:42 +00:00
|
|
|
);
|
2009-05-05 20:33:55 +01:00
|
|
|
|
2008-12-27 22:12:42 +00:00
|
|
|
about->addAuthor(ki18nc("@info:credit", "Volker Lanz"), KLocalizedString(), "vl@fidra.de");
|
|
|
|
about->setHomepage("http://www.partitionmanager.org");
|
|
|
|
|
2010-06-06 19:23:17 +01:00
|
|
|
about->addCredit(ki18n("Hugo Pereira Da Costa"), ki18nc("@info:credit", "Partition Widget Design"), "hugo@oxygen-icons.org");
|
2012-11-05 16:24:49 +00:00
|
|
|
about->addCredit(ki18n("Andrius Štikonas"), ki18nc("@info:credit", "Btrfs support"), "stikonas@gmail.com");
|
2010-06-06 19:23:17 +01:00
|
|
|
|
2008-12-27 22:12:42 +00:00
|
|
|
return about;
|
|
|
|
}
|
|
|
|
|
2009-05-05 20:33:55 +01:00
|
|
|
bool caseInsensitiveLessThan(const QString& s1, const QString& s2)
|
|
|
|
{
|
|
|
|
return s1.toLower() < s2.toLower();
|
|
|
|
}
|
2010-02-17 19:39:24 +00:00
|
|
|
|
2011-06-12 10:31:41 +01:00
|
|
|
bool naturalLessThan(const QString& s1, const QString& s2)
|
|
|
|
{
|
|
|
|
return KStringHandler::naturalCompare(s1, s2) < 0;
|
|
|
|
}
|
|
|
|
|
2010-02-17 19:39:24 +00:00
|
|
|
QIcon createFileSystemColor(FileSystem::Type type, quint32 size)
|
|
|
|
{
|
|
|
|
QPixmap pixmap(size, size);
|
|
|
|
QPainter painter(&pixmap);
|
|
|
|
painter.setPen(QColor(0, 0, 0));
|
|
|
|
painter.setBrush(Config::fileSystemColorCode(type));
|
|
|
|
painter.drawRect(QRect(0, 0, pixmap.width() - 1, pixmap.height() - 1));
|
|
|
|
painter.end();
|
|
|
|
|
|
|
|
return QIcon(pixmap);
|
|
|
|
}
|
2010-03-07 19:02:02 +00:00
|
|
|
|
|
|
|
void showColumnsContextMenu(const QPoint& p, QTreeWidget& tree)
|
|
|
|
{
|
|
|
|
KMenu headerMenu;
|
|
|
|
|
|
|
|
headerMenu.addTitle(i18nc("@title:menu", "Columns"));
|
|
|
|
|
|
|
|
QHeaderView* header = tree.header();
|
|
|
|
|
|
|
|
for (qint32 i = 0; i < tree.model()->columnCount(); i++)
|
|
|
|
{
|
|
|
|
const int idx = header->logicalIndex(i);
|
|
|
|
const QString text = tree.model()->headerData(idx, Qt::Horizontal).toString();
|
|
|
|
|
|
|
|
QAction* action = headerMenu.addAction(text);
|
|
|
|
action->setCheckable(true);
|
|
|
|
action->setChecked(!header->isSectionHidden(idx));
|
|
|
|
action->setData(idx);
|
2010-03-27 13:21:20 +00:00
|
|
|
action->setEnabled(idx > 0);
|
2010-03-07 19:02:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QAction* action = headerMenu.exec(tree.header()->mapToGlobal(p));
|
|
|
|
|
|
|
|
if (action != NULL)
|
|
|
|
{
|
|
|
|
const bool hidden = !action->isChecked();
|
|
|
|
tree.setColumnHidden(action->data().toInt(), hidden);
|
|
|
|
if (!hidden)
|
|
|
|
tree.resizeColumnToContents(action->data().toInt());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-21 10:50:44 +00:00
|
|
|
bool loadBackend()
|
|
|
|
{
|
|
|
|
if (CoreBackendManager::self()->load(Config::backend()) == false)
|
|
|
|
{
|
|
|
|
if (CoreBackendManager::self()->load(CoreBackendManager::defaultBackendName()))
|
|
|
|
{
|
|
|
|
KMessageBox::sorry(NULL,
|
|
|
|
i18nc("@info", "<para>The configured backend plugin \"%1\" could not be loaded.</para>"
|
|
|
|
"<para>Loading the default backend plugin \"%2\" instead.</para>",
|
|
|
|
Config::backend(), CoreBackendManager::defaultBackendName()),
|
|
|
|
i18nc("@title:window", "Error: Could Not Load Backend Plugin"));
|
|
|
|
Config::setBackend(CoreBackendManager::defaultBackendName());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
KMessageBox::error(NULL,
|
|
|
|
i18nc("@info", "<para>Neither the configured (\"%1\") nor the default (\"%2\") backend "
|
|
|
|
"plugin could be loaded.</para><para>Please check your installation.</para>",
|
|
|
|
Config::backend(), CoreBackendManager::defaultBackendName()),
|
|
|
|
i18nc("@title:window", "Error: Could Not Load Backend Plugin"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2010-05-21 19:09:55 +01:00
|
|
|
|
2011-08-29 11:51:42 +01:00
|
|
|
bool checkAccessibleDevices()
|
|
|
|
{
|
|
|
|
if (getSolidDeviceList().empty())
|
|
|
|
{
|
|
|
|
KMessageBox::error(NULL,
|
|
|
|
i18nc("@info", "<para>No usable devices could be found.</para><para>Make sure you have sufficient "
|
|
|
|
"privileges to access block devices on your system.</para>"),
|
|
|
|
i18nc("@title:window", "Error: No Usable Devices Found"));
|
|
|
|
return false;
|
|
|
|
}
|
2013-06-23 10:44:38 +01:00
|
|
|
|
2011-08-29 11:51:42 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-05-21 19:09:55 +01:00
|
|
|
QList<Solid::Device> getSolidDeviceList()
|
|
|
|
{
|
2013-06-23 10:44:38 +01:00
|
|
|
#ifdef ENABLE_UDISKS2
|
|
|
|
QString predicate = "StorageVolume.usage == 'PartitionTable'";
|
|
|
|
|
|
|
|
#else
|
|
|
|
QString predicate = "[ [ [ StorageDrive.driveType == 'HardDisk' OR StorageDrive.driveType == 'CompactFlash'] OR "
|
|
|
|
"[ StorageDrive.driveType == 'MemoryStick' OR StorageDrive.driveType == 'SmartMedia'] ] OR "
|
|
|
|
"[ StorageDrive.driveType == 'SdMmc' OR StorageDrive.driveType == 'Xd'] ]";
|
|
|
|
#endif
|
2010-05-21 19:09:55 +01:00
|
|
|
|
|
|
|
KCmdLineArgs* args = KCmdLineArgs::parsedArgs();
|
|
|
|
if (args->count() > 0)
|
|
|
|
{
|
|
|
|
predicate = " [ " + predicate + " AND ";
|
|
|
|
|
2012-12-09 09:53:43 +00:00
|
|
|
qint32 brackets = (args->count() + 1) / 2;
|
|
|
|
brackets = args->count() == 1 ? 0 : brackets;
|
|
|
|
for (qint32 i = 0; i < brackets; i++)
|
2010-05-21 19:09:55 +01:00
|
|
|
predicate += "[ ";
|
|
|
|
|
2012-12-09 09:53:43 +00:00
|
|
|
bool right_bracket = false;
|
|
|
|
for (qint32 i = 0; i < args->count(); i++, right_bracket =! right_bracket)
|
2010-05-21 19:09:55 +01:00
|
|
|
{
|
|
|
|
predicate += QString("Block.device == '%1' ").arg(args->arg(i));
|
|
|
|
|
2012-12-09 09:53:43 +00:00
|
|
|
if (right_bracket)
|
|
|
|
predicate += i == 1 ? "] " : "] ] ";
|
2010-05-21 19:09:55 +01:00
|
|
|
if (i < args->count() - 1)
|
|
|
|
predicate += "OR ";
|
2012-12-09 09:53:43 +00:00
|
|
|
if (right_bracket && i != args->count() - 2 && i != args->count()-1)
|
|
|
|
predicate += "[ ";
|
2010-05-21 19:09:55 +01:00
|
|
|
}
|
2012-12-09 09:53:43 +00:00
|
|
|
predicate += right_bracket && brackets > 0 ? "] ]" : "]";
|
2010-05-21 19:09:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return Solid::Device::listFromQuery(predicate);
|
|
|
|
}
|