/*************************************************************************** * Copyright (C) 2008, 2009, 2010 by Volker Lanz * * * * 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" #include "backend/corebackendmanager.h" #include "ops/operation.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include void registerMetaTypes() { qRegisterMetaType("Operation*"); qRegisterMetaType("Log::Level"); } void unblockSigChild() { sigset_t sset; sigemptyset(&sset); sigaddset(&sset, SIGCHLD); sigprocmask(SIG_UNBLOCK, &sset, NULL); } static QString suCommand() { KStandardDirs d; const char* candidates[] = { "kdesu", "kdesudo", "gksudo", "gksu" }; QString rval; for (quint32 i = 0; i < sizeof(candidates) / sizeof(candidates[0]); i++) { rval = d.locate("exe", candidates[i]); if (QFileInfo(rval).isExecutable()) return rval; } return QString(); } bool checkPermissions() { if (geteuid() != 0) { // 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 // endless loops of calling the same non-working (kde|gk)su(do) binary again and again. if (!suCommand().isEmpty() && !KCmdLineArgs::parsedArgs()->isSet("dontsu")) { QStringList args = qApp->arguments(); // first argument is our own command again (i.e., argv[0]) if (!args.isEmpty()) args.removeFirst(); // arguments to partition manager must be _one_ argument to (kde|gk)su(do) const QString suArgs = qApp->applicationFilePath() + args.join(" ") + " --dontsu"; if (QProcess::execute(suCommand(), QStringList() << suArgs) == 0) return false; } return KMessageBox::warningContinueCancel(NULL, i18nc("@info", "You do not have administrative privileges." "It is possible to run %1 without these privileges. " "You will, however, not be allowed to apply operations." "Do you want to continue running %1?", KGlobal::mainComponent().aboutData()->programName()), i18nc("@title:window", "No administrative privileges"), KGuiItem(i18nc("@action:button", "Run without administrative privileges")), KStandardGuiItem::cancel(), "runWithoutRootPrivileges") == KMessageBox::Continue; } return true; } KAboutData* createPartitionManagerAboutData() { KAboutData* about = new KAboutData( "partitionmanager", NULL, ki18nc("@title", "KDE Partition Manager"), VERSION, ki18nc("@title", "Manage your disks, partitions and file systems"), KAboutData::License_GPL, ki18nc("@info:credit", "(c) 2008, 2009, 2010 Volker Lanz") ); about->addAuthor(ki18nc("@info:credit", "Volker Lanz"), KLocalizedString(), "vl@fidra.de"); about->setHomepage("http://www.partitionmanager.org"); return about; } bool caseInsensitiveLessThan(const QString& s1, const QString& s2) { return s1.toLower() < s2.toLower(); } 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); } 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); action->setEnabled(true); } 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()); } } bool loadBackend() { if (CoreBackendManager::self()->load(Config::backend()) == false) { if (CoreBackendManager::self()->load(CoreBackendManager::defaultBackendName())) { KMessageBox::sorry(NULL, i18nc("@info", "The configured backend plugin \"%1\" could not be loaded." "Loading the default backend plugin \"%2\" instead.", Config::backend(), CoreBackendManager::defaultBackendName()), i18nc("@title:window", "Error: Could Not Load Backend Plugin")); Config::setBackend(CoreBackendManager::defaultBackendName()); } else { KMessageBox::error(NULL, i18nc("@info", "Neither the configured (\"%1\") nor the default (\"%2\") backend " "plugin could be loaded.Please check your installation.", Config::backend(), CoreBackendManager::defaultBackendName()), i18nc("@title:window", "Error: Could Not Load Backend Plugin")); return false; } } return true; }