[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    KDE/kdelibs
From:       David Faure <faure () kde ! org>
Date:       2008-02-29 21:12:15
Message-ID: 1204319535.343709.14074.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 780723 by dfaure:

Make KOpenWithDialog write to mimeapps.list when asked to remember an association \
between a mimetype and an existing application.

It still has to write a new .desktop file when the user types the name of a program \
without a .desktop file, or when checking "Run in terminal" (but in that case it's \
not a copy of another desktop file, so the previous fix to KConfig::copyTo is now \
unrelated to this code).


 M  +5 -21     kdecore/services/kservice.cpp  
 M  +1 -0      kdecore/services/kservice.h  
 M  +64 -85    kio/kfile/kopenwithdialog.cpp  


--- trunk/KDE/kdelibs/kdecore/services/kservice.cpp #780722:780723
@@ -739,10 +739,9 @@
 QString KService::newServicePath(bool showInMenu, const QString &suggestedName,
                                  QString *menuId, const QStringList \
*reservedMenuIds)  {
-    QString base = suggestedName;
-    if (!showInMenu)
-        base.prepend("kde4-");
+    Q_UNUSED(showInMenu); // TODO KDE5: remove argument
 
+    QString base = suggestedName;
     QString result;
     for(int i = 1; true; i++)
     {
@@ -759,30 +758,15 @@
         if (s)
             continue;
 
-        if (showInMenu)
-        {
-            if (!KStandardDirs::locate("xdgdata-apps", result).isEmpty())
-                continue;
-        }
-        else
-        {
-            QString file = result.mid(5); // Strip "kde4-"
-            if (!KStandardDirs::locate("apps", ".hidden/"+file).isEmpty())
-                continue;
-        }
+        if (!KStandardDirs::locate("xdgdata-apps", result).isEmpty())
+            continue;
 
         break;
     }
     if (menuId)
         *menuId = result;
 
-    if (showInMenu)
-    {
-        return KStandardDirs::locateLocal("xdgdata-apps", result);
-    }
-
-    QString file = result.mid(5); // Strip "kde4-"
-    return KStandardDirs::locateLocal("apps", ".hidden/"+file);
+    return KStandardDirs::locateLocal("xdgdata-apps", result);
 }
 
 bool KService::isApplication() const
--- trunk/KDE/kdelibs/kdecore/services/kservice.h #780722:780723
@@ -455,6 +455,7 @@
      * on @p suggestedName.
      * @param showInMenu true, if the service should be shown in the KDE menu
      *        false, if the service should be hidden from the menu
+     *        This argument isn't used anymore, use NoDisplay=true to hide the \
                service.
      * @param suggestedName name to base the file on, if a service with such
      *        name already exists, a prefix will be added to make it unique.
      * @param menuId If provided, menuId will be set to the menu id to use for
--- trunk/KDE/kdelibs/kio/kfile/kopenwithdialog.cpp #780722:780723
@@ -40,7 +40,6 @@
 #include <krun.h>
 #include <kstandarddirs.h>
 #include <kstringhandler.h>
-#include <kmimetypetrader.h>
 #include <kurlcompletion.h>
 #include <kurlrequester.h>
 #include <kmimetype.h>
@@ -433,6 +432,8 @@
      */
     void setMimeType(const KUrl::List &_urls);
 
+    void addToMimeAppsList(const QString& serviceId);
+
     /**
      * Create a dialog that asks for a application to open a given
      * URL(s) with.
@@ -706,7 +707,7 @@
 
 void KOpenWithDialog::slotTextChanged()
 {
-    kDebug(250)<<"KOpenWithDialog::slotTextChanged";
+    kDebug(250)<<"KOpenWithDialog::slotTextChanged" << d->edit->url();
     // Forget about the service
     d->curService = 0L;
     enableButton(Ok, !d->edit->url().isEmpty());
@@ -749,6 +750,30 @@
     return exec.simplified();
 }
 
+void KOpenWithDialogPrivate::addToMimeAppsList(const QString& serviceId)
+{
+    KSharedConfig::Ptr profile = KSharedConfig::openConfig("mimeapps.list", \
KConfig::NoGlobals, "xdgdata-apps"); +    KConfigGroup addedApps(profile, "Added \
Associations"); +    QStringList apps = addedApps.readXdgListEntry(qMimeType);
+    apps.removeAll(serviceId);
+    apps.prepend(serviceId); // make it the preferred app
+    addedApps.writeXdgListEntry(qMimeType, apps);
+    addedApps.sync();
+
+    // Also make sure the "auto embed" setting for this mimetype is off
+    KSharedConfig::Ptr fileTypesConfig = KSharedConfig::openConfig("filetypesrc", \
KConfig::NoGlobals); +    \
fileTypesConfig->group("EmbedSettings").writeEntry(QString("embed-")+qMimeType, \
false); +    fileTypesConfig->sync();
+
+    kDebug(250) << "rebuilding ksycoca...";
+
+    // kbuildsycoca is the one reading mimeapps.list, so we need to run it now
+    KBuildSycocaProgressDialog::rebuildKSycoca(q);
+
+    m_pService = KService::serviceByMenuId(serviceId);
+    Q_ASSERT( m_pService );
+}
+
 void KOpenWithDialogPrivate::_k_slotOK()
 {
     QString typedExec(edit->url().pathOrUrl());
@@ -814,97 +839,51 @@
     if ( m_pService && terminal->isChecked() != m_pService->terminal() )
         m_pService = 0; // It's not exactly this service we're running
 
-    const bool bRemember = remember && remember->isChecked();
-    if (!bRemember && m_pService) {
-        q->accept();
-        return;
-    }
 
-    const bool createDesktopFile = bRemember || saveNewApps;
-
-    if (!createDesktopFile) {
-        // Create temp service
-        m_pService = new KService(initialServiceName, fullExec, QString());
-        if (terminal->isChecked()) {
-            m_pService->setTerminal(true);
-            // only add --noclose when we are sure it is konsole we're using
-            if (preferredTerminal == "konsole" && nocloseonexit->isChecked())
-                m_pService->setTerminalOptions("--noclose");
-        }
-        q->accept();
-        return;
-    }
-
-    // if we got here, we can't seem to find a service for what they
-    // wanted.  The other possibility is that they have asked for the
-    // association to be remembered.  Create/update service.
-
-    QString newPath;
-    QString oldPath;
-    QString menuId;
+    const bool bRemember = remember && remember->isChecked();
+    kDebug(250) << "bRemember=" << bRemember << "service found=" << m_pService;
     if (m_pService) {
-        oldPath = m_pService->entryPath();
-        newPath = m_pService->locateLocal();
-        menuId = m_pService->menuId();
-        kDebug(250) << "Updating existing service" << m_pService->entryPath() << "(" \
<< newPath << ")"; +        if (bRemember) {
+            // Associate this app with qMimeType in mimeapps.list
+            Q_ASSERT(!qMimeType.isEmpty()); // we don't show the remember checkbox \
otherwise +            addToMimeAppsList(m_pService->storageId());
+        }
     } else {
-        newPath = KService::newServicePath(false /* hidden */, serviceName, \
                &menuId);
-        kDebug(250) << "Creating new service" << serviceName << "(" << newPath << \
                ")";
-    }
+        const bool createDesktopFile = bRemember || saveNewApps;
+        if (!createDesktopFile) {
+            // Create temp service
+            m_pService = new KService(initialServiceName, fullExec, QString());
+            if (terminal->isChecked()) {
+                m_pService->setTerminal(true);
+                // only add --noclose when we are sure it is konsole we're using
+                if (preferredTerminal == "konsole" && nocloseonexit->isChecked())
+                    m_pService->setTerminalOptions("--noclose");
+            }
+        } else {
+            // If we got here, we can't seem to find a service for what they wanted. \
Create one.  
-    int maxPreference = 1;
-    if (!qMimeType.isEmpty()) {
-        // Find max highest preference from either initial preference or from \
                profile
-        const KServiceOfferList offerList = KMimeTypeTrader::self()->weightedOffers( \
                qMimeType );
-        if (!offerList.isEmpty())
-            maxPreference = offerList.first().preference();
-    }
+            QString menuId;
+            QString newPath = KService::newServicePath(false /* ignored argument */, \
serviceName, &menuId); +            kDebug(250) << "Creating new service" << \
serviceName << "(" << newPath << ")";  
-    KDesktopFile *desktop = 0;
-    if (!oldPath.isEmpty() && (oldPath != newPath)) {
-        KDesktopFile orig( oldPath );
-        desktop = orig.copyTo( newPath );
-    } else {
-        desktop = new KDesktopFile(newPath);
-    }
-    KConfigGroup cg = desktop->desktopGroup();
-    cg.writeEntry("Type", "Application");
-    cg.writeEntry("Name", initialServiceName);
-    cg.writePathEntry("Exec", fullExec);
-    if (terminal->isChecked()) {
-        cg.writeEntry("Terminal", true);
-        // only add --noclose when we are sure it is konsole we're using
-        if (preferredTerminal == "konsole" && nocloseonexit->isChecked())
-            cg.writeEntry("TerminalOptions", "--noclose");
-    } else {
-        cg.writeEntry("Terminal", false);
-    }
-    cg.writeEntry("InitialPreference", maxPreference + 1);
+            KDesktopFile desktopFile(newPath);
+            KConfigGroup cg = desktopFile.desktopGroup();
+            cg.writeEntry("Type", "Application");
+            cg.writeEntry("Name", initialServiceName);
+            cg.writePathEntry("Exec", fullExec);
+            cg.writeEntry("NoDisplay", true); // don't make it appear in the K menu
+            if (terminal->isChecked()) {
+                cg.writeEntry("Terminal", true);
+                // only add --noclose when we are sure it is konsole we're using
+                if (preferredTerminal == "konsole" && nocloseonexit->isChecked())
+                    cg.writeEntry("TerminalOptions", "--noclose");
+            }
+            cg.writeXdgListEntry("MimeType", QStringList() << qMimeType);
+            cg.sync();
 
-
-    if (createDesktopFile && !qMimeType.isEmpty()) {
-        // Add mimetype to the service desktop file
-        QStringList mimeList = cg.readXdgListEntry("MimeType");
-        if (!mimeList.contains(qMimeType)) {
-            mimeList.append(qMimeType);
-            cg.writeXdgListEntry("MimeType", mimeList);
+            addToMimeAppsList(menuId);
         }
-
-        // Also make sure the "auto embed" setting for this mimetype is off
-        KSharedConfig::Ptr fileTypesConfig = \
                KSharedConfig::openConfig("filetypesrc", KConfig::NoGlobals);
-        fileTypesConfig->group("EmbedSettings").writeEntry(QString("embed-")+qMimeType, \
                false);
-        fileTypesConfig->sync();
     }
-
-    // write it all out to the file
-    cg.sync();
-    delete desktop;
-
-    KBuildSycocaProgressDialog::rebuildKSycoca(q);
-
-    m_pService = KService::serviceByMenuId( menuId );
-    Q_ASSERT( m_pService );
-
     q->accept();
 }
 


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic