Advertisement
Guest User

krun with extended url handling

a guest
May 16th, 2015
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.31 KB | None | 0 0
  1. static QString schemeHandler(const QString &protocol, const QUrl &url)
  2. {
  3.     // We have up to two sources of data, for protocols not handled by kioslaves (so called "helper") :
  4.     // 1) the exec line of the .protocol file, if there's one
  5.     // 2) the application associated with x-scheme-handler/<protocol> if there's one
  6.  
  7.     // If both exist, then:
  8.     //  A) if the .protocol file says "launch an application", then the new-style handler-app has priority
  9.     //  B) but if the .protocol file is for a kioslave (e.g. kio_http) then this has priority over
  10.     //     firefox or chromium saying x-scheme-handler/http. Gnome people want to send all HTTP urls
  11.     //     to a webbrowser, but we want mimetype-determination-in-calling-application by default
  12.     //     (the user can configure a BrowserApplication though)
  13.  
  14.     QString b_mime_type = QString::fromLatin1("x-scheme-handler/") + protocol;
  15.     QString e_mime_type('');
  16.  
  17.     QRegExp *regExp = 0;
  18.  
  19.  
  20.     QMimeDatabase db;
  21.  
  22.     foreach (QMimeType mt, db.allMimeTypes()) {
  23.         if (mt.name().startsWith("x-scheme-handler/") && mt.parentMimeTypes().contains(b_mime_type)) {
  24.             if(!regExp) {
  25.                 regExp = new QRegExp();
  26.                 regExp->setCaseSensitivity(Qt::CaseInsensitive);
  27.                 regExp->setMinimal(false);
  28.                 regExp->setPatternSyntax(QRegExp::WildcardUnix);
  29.             }
  30.             foreach (QString s, mt.globPatterns()) {
  31.                 regExp->setPattern(s);
  32.                 if (regExp.isValid() && regExp->exactMatch(url.url())) {
  33.                     e_mime_type = mt.name();
  34.                 }
  35.             }
  36.         }
  37.     }
  38.     if (regExp)
  39.         delete regExp;
  40.  
  41.     const KService::Ptr service;
  42.     if (!e_mime_type.isEmpty()) {
  43.         service = KMimeTypeTrader::self()->preferredService(e_mime_type);
  44.         if (service) {
  45.             return service->exec(); // for helper protocols, the handler app has priority over the hardcoded one (see A above)
  46.         }
  47.     }
  48.     service = KMimeTypeTrader::self()->preferredService(b_mime_type);
  49.     if (service) {
  50.         return service->exec(); // for helper protocols, the handler app has priority over the hardcoded one (see A above)
  51.     }
  52.     Q_ASSERT(KProtocolInfo::isHelperProtocol(protocol));
  53.     return KProtocolInfo::exec(protocol);
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement