bool IconThemesConfig::installThemes(const QStringList &themes, const QString &archiveName) { bool everythingOk = true; QString localThemesDir(locateLocal("icon", "./")); KProgressDialog progressDiag(this, "themeinstallprogress", i18n("Installing icon themes"), QString::null, true); progressDiag.setAutoClose(true); progressDiag.progressBar()->setTotalSteps(themes.count()); progressDiag.show(); KTar archive(archiveName); archive.open(IO_ReadOnly); kapp->processEvents(); const KArchiveDirectory* rootDir = archive.directory(); KArchiveDirectory* currentTheme; for (QStringList::ConstIterator it = themes.begin(); it != themes.end(); ++it) { progressDiag.setLabel( i18n("<qt>Installing <strong>%1</strong> theme</qt>") .arg(*it)); kapp->processEvents(); if (progressDiag.wasCancelled()) break; currentTheme = dynamic_cast<KArchiveDirectory*>( const_cast<KArchiveEntry*>( rootDir->entry(*it))); if (currentTheme == NULL) { // we tell back that something went wrong, but try to install as much // as possible everythingOk = false; continue; } currentTheme->copyTo(localThemesDir + *it); progressDiag.progressBar()->advance(1); } archive.close(); return everythingOk; }
// Theme installation code inspired by kcm_icon void KDMThemeWidget::installNewTheme() { QString url; KUrlRequesterDialog fileRequester(url, i18n("Drag or Type Theme URL"), this); fileRequester.urlRequester()->setMode(KFile::File | KFile::Directory | KFile::ExistingOnly); KUrl themeURL = fileRequester.getUrl(); if (themeURL.isEmpty()) return; #if 0 if (themeURL.isLocalFile() && QDir(themeURL.toLocalFile()).exists()) { insertTheme(themeURL.toLocalFile()); emit changed(); return; } #endif QString themeTmpFile; if (!KIO::NetAccess::download(themeURL, themeTmpFile, this)) { QString sorryText; if (themeURL.isLocalFile()) sorryText = i18n("Unable to find the KDM theme archive %1.", themeURL.prettyUrl()); else sorryText = i18n("Unable to download the KDM theme archive;\n" "please check that address %1 is correct.", themeURL.prettyUrl()); KMessageBox::sorry(this, sorryText); return; } QList<const KArchiveDirectory *> foundThemes; KTar archive(themeTmpFile); archive.open(QIODevice::ReadOnly); const KArchiveDirectory *archDir = archive.directory(); foreach (const QString &ent, archDir->entries()) { const KArchiveEntry *possibleDir = archDir->entry(ent); if (possibleDir->isDirectory()) { const KArchiveDirectory *subDir = static_cast<const KArchiveDirectory *>(possibleDir); if (subDir->entry("KdmGreeterTheme.desktop")) foundThemes.append(subDir); } } if (foundThemes.isEmpty()) KMessageBox::error(this, i18n("The file is not a valid KDM theme archive.")); else { KProgressDialog progressDiag(this, i18nc("@title:window", "Installing KDM themes"), QString()); progressDiag.setModal(true); progressDiag.setAutoClose(true); progressDiag.progressBar()->setMaximum(foundThemes.size()); progressDiag.show(); KTempDir themesTempDir(KStandardDirs::locateLocal("tmp", "kdmthemes")); QStringList themesTempDirs, themesDirs; foreach (const KArchiveDirectory *ard, foundThemes) { progressDiag.setLabelText( i18nc("@info:progress", "<qt>Unpacking <strong>%1</strong> theme</qt>", ard->name())); themesTempDirs.append(themesTempDir.name() + ard->name()); themesDirs.append(themeDir + ard->name()); ard->copyTo(themesTempDirs.last(), true); progressDiag.progressBar()->setValue(progressDiag.progressBar()->value() + 1); if (progressDiag.wasCancelled()) break; } progressDiag.setLabelText(i18nc("@info:progress", "<qt>Installing the themes</qt>")); QVariantMap helperargs; QVariantMap returnedData; helperargs["subaction"] = Helper::InstallThemes; helperargs["themes"] = themesTempDirs; if (executeThemeAction(parentWidget(), helperargs, &returnedData)) { QString errorMessage = i18n("There were errors while installing the following themes:\n"); QStringList failedThemes = returnedData["failedthemes"].toStringList(); foreach (const QString &path, failedThemes) errorMessage += path + '\n'; KMessageBox::error(this, errorMessage); }