void FavoriteAppsModel::setConfig(const KSharedConfig::Ptr &ptr) { m_config = ptr; KConfigGroup baseGroup(m_config, "favorites"); // get all the favorites QMap<int, KService::Ptr> favoriteMap; foreach (const QString &favoriteGroup, baseGroup.groupList()) { if (favoriteGroup.startsWith("favorite-")) { KConfigGroup favoriteConfig(&baseGroup, favoriteGroup); int rank = favoriteGroup.split("-").last().toInt(); QString id = favoriteConfig.readEntry("serviceId"); KService::Ptr service = KService::serviceByStorageId(id); if (!service.isNull()) { favoriteMap.insert(rank, service); } } } beginResetModel(); m_favoriteList.clear(); auto it = favoriteMap.constBegin(), end = favoriteMap.constEnd(); for (; it != end; ++it) { FavoriteInfo info = { it.key(), it.value() }; m_favoriteList << info; } endResetModel(); countChanged(); }
void FavoriteAppsModel::addFavorite(const QString &favoriteId) { QString serviceId = serviceIdFromFavoriteId(favoriteId); if (serviceId.isEmpty()) { return; } KService::Ptr service = KService::serviceByStorageId(serviceId); if (service.isNull()) { kWarning() << "Could not find a service for" << serviceId; return; } int rank; if (!m_favoriteList.isEmpty()) { rank = m_favoriteList.last().rank + 1; } else { rank = 0; } FavoriteInfo info = { rank, service }; int row = m_favoriteList.count(); beginInsertRows(QModelIndex(), row, row); m_favoriteList << info; endInsertRows(); countChanged(); KConfigGroup baseGroup(m_config, "favorites"); KConfigGroup group(&baseGroup, QString("favorite-%1").arg(rank)); group.writeEntry("serviceId", serviceId); baseGroup.sync(); }
bool FavoriteAppsModel::trigger(int row) { KService::Ptr service = m_favoriteList.value(row).service; if (service.isNull()) { kWarning() << "Invalid row"; return false; } return KRun::run(*service, KUrl::List(), 0); }
void FileManager::openFile( const QString& fileName, const QString& name, const QString& title, const QString& partName, const QVariantList& partParams ) { if( fileName.isEmpty() ) return; QString fullName = name.isEmpty() ? KUrl( fileName ).fileName() : name; QString fullTitle = title.isEmpty() ? fullName : title; if( d->parts.contains( fullName ) ) { emit newPart( fullName, fullTitle ); return; } KMimeType::Ptr mime = KMimeType::findByPath( fileName ); KParts::ReadOnlyPart* part = 0; KService::List parts; if( !partName.isEmpty() ) { KService::Ptr service = KService::serviceByDesktopName( partName ); if( !service.isNull() ) parts.append( service ); } if( parts.count() == 0 ) { parts.append( KMimeTypeTrader::self()->query( mime->name(), "KParts/ReadWritePart" ) ); parts.append( KMimeTypeTrader::self()->query( mime->name(), "KParts/ReadOnlyPart" ) ); if( mime->name().contains( "audio" ) && parts.count() == 0 ) parts.append( KService::serviceByStorageId( "dragonplayer_part.desktop" ) ); } if( parts.count() > 0 ) { part = parts.first()->createInstance<KParts::ReadWritePart>( 0, partParams ); if( !part ) part = parts.first()->createInstance<KParts::ReadOnlyPart>( 0, partParams ); } if( part ) { // Add the part if it is found KUrl url( fileName ); part->openUrl( url ); d->parts.insert( fullName, part ); d->partManager->addPart( part, true ); emit newPart( fullName, fullTitle ); return; } // There really is no part that can be used. // Instead, just open it in an external application. // KRun* runner = new KRun( KUrl( fileName ), qApp->activeWindow() ); }
bool KProtocolInfo::isHelperProtocol( const QString &protocol ) { // We call the findProtocol directly (not via KProtocolManager) to bypass any proxy settings. KProtocolInfo::Ptr prot = KProtocolInfoFactory::self()->findProtocol(protocol); if ( prot ) return prot->m_isHelperProtocol; const KService::Ptr service = KMimeTypeTrader::self()->preferredService(QString::fromLatin1("x-scheme-handler/") + protocol); return !service.isNull(); }
void KMimeTypeTest::testHasServiceType2() // with services coming from ksycoca { KService::Ptr katepart = KService::serviceByDesktopPath( "katepart.desktop" ); QVERIFY( !katepart.isNull() ); QVERIFY( katepart->hasMimeType( "text/plain" ) ); QVERIFY( katepart->hasMimeType( "text/x-patch" ) ); // due to inheritance QVERIFY( !katepart->hasMimeType( "image/png" ) ); QVERIFY( katepart->hasServiceType( "KParts/ReadOnlyPart" ) ); QVERIFY( katepart->hasServiceType( "KParts/ReadWritePart" ) ); QVERIFY( !katepart->hasServiceType( "KTextEditor/Plugin" ) ); KService::Ptr ktexteditor_insertfile = KService::serviceByDesktopPath( "ktexteditor_insertfile.desktop" ); QVERIFY( !ktexteditor_insertfile.isNull() ); QVERIFY( ktexteditor_insertfile->hasServiceType( "KTextEditor/Plugin" ) ); QVERIFY( !ktexteditor_insertfile->hasServiceType( "KParts/ReadOnlyPart" ) ); }
QVariant FavoriteAppsModel::data(const QModelIndex &index, int role) const { KService::Ptr service = m_favoriteList.value(index.row()).service; if (service.isNull()) { return QVariant(); } if (role == Qt::DisplayRole) { return service->name(); } else if (role == Qt::DecorationRole) { return KIcon(service->icon()); } else if (role == FavoriteIdRole) { return QVariant("app:" + service->storageId()); } else { kWarning() << "Unhandled role" << role; return QVariant(); } }
void ArkViewer::view(const QString& fileName, QWidget *parent) { KMimeType::Ptr mimeType = KMimeType::findByPath(fileName); kDebug() << "MIME type" << mimeType->name(); KService::Ptr viewer = ArkViewer::getViewer(mimeType); const bool needsExternalViewer = (!viewer.isNull() && !viewer->hasServiceType(QLatin1String("KParts/ReadOnlyPart"))); if (needsExternalViewer) { // We have already resolved the MIME type and the service above. // So there is no point in using KRun::runUrl() which would need // to do the same again. const KUrl::List fileUrlList = KUrl(fileName); // The last argument (tempFiles) set to true means that the temporary // file will be removed when the viewer application exits. KRun::run(*viewer, fileUrlList, parent, true); return; } bool viewInInternalViewer = true; if (viewer.isNull()) { // No internal viewer available for the file. Ask the user if it // should be previewed as text/plain. int response; if (!mimeType->isDefault()) { // File has a defined MIME type, and not the default // application/octet-stream. So it could be viewable as // plain text, ask the user. response = KMessageBox::warningContinueCancel(parent, i18n("The internal viewer cannot preview this type of file<nl/>(%1).<nl/><nl/>Do you want to try to view it as plain text?", mimeType->name()), i18nc("@title:window", "Cannot Preview File"), KGuiItem(i18nc("@action:button", "Preview as Text"), KIcon(QLatin1String("text-plain"))), KStandardGuiItem::cancel(), QString(QLatin1String("PreviewAsText_%1")).arg(mimeType->name())); } else { // No defined MIME type, or the default application/octet-stream. // There is still a possibility that it could be viewable as plain // text, so ask the user. Not the same as the message/question // above, because the wording and default are different. response = KMessageBox::warningContinueCancel(parent, i18n("The internal viewer cannot preview this unknown type of file.<nl/><nl/>Do you want to try to view it as plain text?"), i18nc("@title:window", "Cannot Preview File"), KGuiItem(i18nc("@action:button", "Preview as Text"), KIcon(QLatin1String("text-plain"))), KStandardGuiItem::cancel(), QString(), KMessageBox::Dangerous); } if (response == KMessageBox::Cancel) { viewInInternalViewer = false; } else { // set for viewer later mimeType = KMimeType::mimeType(QLatin1String("text/plain")); } } if (viewInInternalViewer) { ArkViewer *internalViewer = new ArkViewer(parent, Qt::Window); if (internalViewer->viewInInternalViewer(fileName, mimeType)) { internalViewer->show(); // The internal viewer is showing the file, and will // remove the temporary file in dialogClosed(). So there // is no more to do here. return; } else { KMessageBox::sorry(parent, i18n("The internal viewer cannot preview this file.")); delete internalViewer; } } // Only get here if there is no internal viewer available or could be // used for the file, and no external viewer was opened. Nothing can be // done with the temporary file, so remove it now. QFile::remove(fileName); }
void WebPage::handleUnsupportedContent(QNetworkReply *reply) { Q_ASSERT(reply); // Put the job on hold... #if KDE_IS_VERSION( 4, 5, 96) kDebug() << "PUT REPLY ON HOLD..."; KIO::Integration::AccessManager::putReplyOnHold(reply); #else reply->abort(); #endif // This is probably needed just in ONE stupid case.. if (_protHandler.postHandling(reply->request(), mainFrame())) { kDebug() << "POST HANDLING the unsupported..."; return; } if (reply->error() != QNetworkReply::NoError) return; // get reply url... KUrl replyUrl = reply->url(); bool isLocal = replyUrl.isLocalFile(); if(isLocal && KProtocolInfo::isKnownProtocol(replyUrl)) { kDebug() << "WARNING: launching a new app..."; new KRun(replyUrl, rApp->mainWindow()); // No need to delete KRun, it autodeletes itself return; } // Get suggested file name... extractSuggestedFileName(reply, _suggestedFileName); // Get mimeType... extractMimeType(reply, _mimeType); // Convert executable text files to plain text... if (KParts::BrowserRun::isTextExecutable(_mimeType)) _mimeType = QL1S("text/plain"); kDebug() << "Detected MimeType = " << _mimeType; kDebug() << "Suggested File Name = " << _suggestedFileName; // ------------------------------------------------ KService::Ptr appService = KMimeTypeTrader::self()->preferredService(_mimeType); if (appService.isNull()) // no service can handle this. We can just download it.. { kDebug() << "no service can handle this. We can just download it.."; isLocal ? KMessageBox::sorry(view(), i18n("No service can handle this file.")) : downloadReply(reply, _suggestedFileName); return; } if (!isLocal) { KParts::BrowserOpenOrSaveQuestion dlg(rApp->mainWindow(), replyUrl, _mimeType); if (!_suggestedFileName.isEmpty()) dlg.setSuggestedFileName(_suggestedFileName); switch (dlg.askEmbedOrSave()) { case KParts::BrowserOpenOrSaveQuestion::Save: kDebug() << "user choice: no services, just download!"; downloadReply(reply, _suggestedFileName); return; case KParts::BrowserOpenOrSaveQuestion::Cancel: return; default: // non extant case break; } } // Handle Post operations that return content... if (reply->operation() == QNetworkAccessManager::PostOperation) { kDebug() << "POST OPERATION: downloading file..."; QFileInfo finfo(_suggestedFileName.isEmpty() ? _loadingUrl.fileName() : _suggestedFileName); KTemporaryFile tempFile; tempFile.setSuffix(QL1C('.') + finfo.suffix()); tempFile.setAutoRemove(false); tempFile.open(); KUrl destUrl; destUrl.setPath(tempFile.fileName()); kDebug() << "First save content to" << destUrl; KIO::Job *job = KIO::file_copy(_loadingUrl, destUrl, 0600, KIO::Overwrite); job->ui()->setWindow(rApp->mainWindow()); connect(job, SIGNAL(result(KJob *)), this, SLOT(copyToTempFileResult(KJob*))); return; }