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"
|
|
|
|
|
2014-05-18 21:41:48 +01:00
|
|
|
#include <KAboutData>
|
|
|
|
#include <KMessageBox>
|
2014-05-13 23:53:37 +01:00
|
|
|
#include <KLocalizedString>
|
2009-11-23 18:27:21 +00:00
|
|
|
|
2014-05-18 21:41:48 +01:00
|
|
|
#include <QAction>
|
2009-11-23 18:27:21 +00:00
|
|
|
#include <QApplication>
|
2014-05-18 21:41:48 +01:00
|
|
|
#include <QCollator>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QIcon>
|
2014-05-17 03:20:43 +01:00
|
|
|
#include <QMenu>
|
2014-05-18 21:41:48 +01:00
|
|
|
#include <QHeaderView>
|
2010-02-17 19:39:24 +00:00
|
|
|
#include <QPainter>
|
|
|
|
#include <QPixmap>
|
2014-05-18 21:41:48 +01:00
|
|
|
#include <QProcess>
|
|
|
|
#include <QStandardPaths>
|
2010-02-17 19:39:24 +00:00
|
|
|
#include <QRect>
|
2010-03-07 19:02:02 +00:00
|
|
|
#include <QTreeWidget>
|
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()
|
|
|
|
{
|
2014-05-20 22:33:02 +01:00
|
|
|
const QString candidates[] = { QStringLiteral("kdesu"), QStringLiteral("kdesudo"), QStringLiteral("gksudo"), QStringLiteral("gksu") };
|
2009-11-23 21:23:28 +00:00
|
|
|
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++)
|
|
|
|
{
|
2014-05-18 21:41:48 +01:00
|
|
|
rval = QStandardPaths::findExecutable(candidates[i]);
|
2009-11-23 21:23:28 +00:00
|
|
|
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-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.
|
2014-05-18 21:41:48 +01:00
|
|
|
if (!suCommand().isEmpty() && !QCoreApplication::arguments().contains(QLatin1String("--dontsu")))
|
2009-11-23 18:27:21 +00:00
|
|
|
{
|
2015-02-04 23:19:49 +00:00
|
|
|
QString argList;
|
2012-02-04 10:14:13 +00:00
|
|
|
|
|
|
|
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.
|
2014-05-20 22:33:02 +01:00
|
|
|
if (suCmd.indexOf(QStringLiteral("kdesu")) != -1)
|
2015-02-04 23:19:49 +00:00
|
|
|
argList = QStringLiteral("-c ");
|
2012-02-04 10:14:13 +00:00
|
|
|
|
2015-02-04 23:19:49 +00:00
|
|
|
argList += QCoreApplication::arguments().join(QStringLiteral(" ")) + QStringLiteral(" --dontsu");
|
2012-02-04 10:14:13 +00:00
|
|
|
|
2015-02-04 23:19:49 +00:00
|
|
|
if (QProcess::execute(suCmd, QStringList(argList)) == 0)
|
2009-11-23 18:27:21 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-06-23 10:44:38 +01:00
|
|
|
|
2014-05-18 21:41:48 +01:00
|
|
|
return KMessageBox::warningContinueCancel(NULL, xi18nc("@info",
|
2008-12-27 22:12:42 +00:00
|
|
|
"<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>",
|
2014-05-18 21:41:48 +01:00
|
|
|
QGuiApplication::applicationDisplayName()),
|
2008-12-27 22:12:42 +00:00
|
|
|
i18nc("@title:window", "No administrative privileges"),
|
2014-05-20 22:33:02 +01:00
|
|
|
KGuiItem(i18nc("@action:button", "Run without administrative privileges"), QStringLiteral("arrow-right")),
|
2008-12-27 22:12:42 +00:00
|
|
|
KStandardGuiItem::cancel(),
|
2014-05-20 22:33:02 +01:00
|
|
|
QStringLiteral("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(
|
2014-05-18 21:41:48 +01:00
|
|
|
QStringLiteral("partitionmanager"),
|
|
|
|
xi18nc("@title", "<application>KDE Partition Manager</application>"),
|
|
|
|
QStringLiteral(VERSION),
|
|
|
|
i18nc("@title", "Manage your disks, partitions and file systems"),
|
2015-02-05 01:32:14 +00:00
|
|
|
KAboutLicense::GPL_V3,
|
2014-07-24 08:56:54 +01:00
|
|
|
i18nc("@info:credit", "© 2008-2013 Volker Lanz"));
|
2014-05-18 21:41:48 +01:00
|
|
|
about->setOrganizationDomain(QByteArray("kde.org"));
|
|
|
|
about->setProductName(QByteArray("partitionmanager"));
|
|
|
|
|
2014-07-02 14:21:47 +01:00
|
|
|
about->addAuthor(i18nc("@info:credit", "Volker Lanz"), i18nc("@info:credit", "Former maintainer"));
|
2015-02-05 01:12:27 +00:00
|
|
|
about->addAuthor(i18nc("@info:credit", "Andrius Štikonas"), i18nc("@info:credit", "Maintainer"), QStringLiteral("andrius@stikonas.eu"));
|
2014-05-18 21:41:48 +01:00
|
|
|
about->setHomepage(QStringLiteral("http://www.partitionmanager.org"));
|
|
|
|
|
|
|
|
about->addCredit(i18n("Hugo Pereira Da Costa"), i18nc("@info:credit", "Partition Widget Design"), QStringLiteral("hugo@oxygen-icons.org"));
|
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)
|
|
|
|
{
|
2014-05-18 21:41:48 +01:00
|
|
|
QCollator c;
|
|
|
|
c.setNumericMode(true);
|
|
|
|
c.setCaseSensitivity(Qt::CaseSensitive);
|
|
|
|
return c.compare(s1, s2) < 0;
|
2011-06-12 10:31:41 +01:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2014-05-17 03:20:43 +01:00
|
|
|
QMenu headerMenu(i18nc("@title:menu", "Columns"));
|
2010-03-07 19:02:02 +00:00
|
|
|
|
|
|
|
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,
|
2014-05-18 21:41:48 +01:00
|
|
|
xi18nc("@info", "<para>The configured backend plugin \"%1\" could not be loaded.</para>"
|
2010-03-21 10:50:44 +00:00
|
|
|
"<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,
|
2014-05-18 21:41:48 +01:00
|
|
|
xi18nc("@info", "<para>Neither the configured (\"%1\") nor the default (\"%2\") backend "
|
2010-03-21 10:50:44 +00:00
|
|
|
"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;
|
|
|
|
}
|