]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/views/viewproperties.cpp
Also reconnect signals for the directory lister when changing the view or the URL...
[dolphin.git] / src / views / viewproperties.cpp
index 483fc0be76deca211dfea6d97e85b9c8082e261d..ed055e09e2f7fc93a4e2bdd2f781537f31d8bf6a 100644 (file)
 
 #include "settings/dolphinsettings.h"
 
+namespace {
+    // String representation to mark the additional properties of
+    // the details view as customized by the user. See
+    // ViewProperties::additionalInfoV2() for more information.
+    const char* CustomizedDetailsString = "CustomizedDetails";
+}
+
 ViewProperties::ViewProperties(const KUrl& url) :
     m_changedProps(false),
     m_autoSave(true),
@@ -46,15 +53,19 @@ ViewProperties::ViewProperties(const KUrl& url) :
     // We try and save it to the file .directory in the directory being viewed.
     // If the directory is not writable by the user or the directory is not local,
     // we store the properties information in a local file.
-    const bool isSearchUrl = url.protocol().contains("search");
-    if (isSearchUrl) {
+    bool useDetailsViewWithPath = false;
+    if (url.protocol().contains("search")) {
         m_filePath = destinationDir("search");
+        useDetailsViewWithPath = true;
+    } else if (url.protocol() == QLatin1String("trash")) {
+        m_filePath = destinationDir("trash");
+        useDetailsViewWithPath = true;
     } else if (useGlobalViewProps) {
         m_filePath = destinationDir("global");
     } else if (url.isLocalFile()) {
         m_filePath = url.toLocalFile();
         const QFileInfo info(m_filePath);
-        if (!info.isWritable()) {
+        if (!info.isWritable() || !isPartOfHome(m_filePath)) {
             m_filePath = destinationDir("local") + m_filePath;
         }
     } else {
@@ -66,11 +77,11 @@ ViewProperties::ViewProperties(const KUrl& url) :
 
     // If the .directory file does not exist or the timestamp is too old,
     // use default values instead.
-    const bool useDefaultProps = (!useGlobalViewProps || isSearchUrl) &&
+    const bool useDefaultProps = (!useGlobalViewProps || useDetailsViewWithPath) &&
                                  (!QFileInfo(file).exists() ||
                                   (m_node->timestamp() < settings->viewPropsTimestamp()));
     if (useDefaultProps) {
-        if (isSearchUrl) {
+        if (useDetailsViewWithPath) {
             setViewMode(DolphinView::DetailsView);
             setAdditionalInfo(KFileItemDelegate::InformationList() << KFileItemDelegate::LocalPathOrUrl);
         } else {
@@ -224,6 +235,17 @@ void ViewProperties::setAdditionalInfo(const KFileItemDelegate::InformationList&
         if (m_node->version() < 2) {
             m_node->setVersion(2);
         }
+
+        const bool markCustomizedDetails = (m_node->viewMode() == DolphinView::DetailsView)
+                                           && !newInfoStringList.contains(CustomizedDetailsString);
+        if (markCustomizedDetails) {
+            // The additional information of the details-view has been modified. Set a marker,
+            // so that it is allowed to also show no additional information
+            // (see fallback in ViewProperties::additionalInfoV2, if no additional information is
+            // available).
+            newInfoStringList.append(CustomizedDetailsString);
+        }
+
         m_node->setAdditionalInfoV2(newInfoStringList);
         update();
     }
@@ -231,15 +253,15 @@ void ViewProperties::setAdditionalInfo(const KFileItemDelegate::InformationList&
 
 KFileItemDelegate::InformationList ViewProperties::additionalInfo() const
 {
-    KFileItemDelegate::InformationList usedInfos;
+    KFileItemDelegate::InformationList usedInfo;
 
     switch (m_node->version()) {
-    case 1: usedInfos = additionalInfoV1(); break;
-    case 2: usedInfos = additionalInfoV2(); break;
+    case 1: usedInfo = additionalInfoV1(); break;
+    case 2: usedInfo = additionalInfoV2(); break;
     default: kWarning() << "Unknown version of the view properties";
     }
 
-    return usedInfos;
+    return usedInfo;
 }
 
 
@@ -310,7 +332,7 @@ QString ViewProperties::destinationDir(const QString& subDir) const
 
 KFileItemDelegate::InformationList ViewProperties::additionalInfoV1() const
 {
-    KFileItemDelegate::InformationList usedInfos;
+    KFileItemDelegate::InformationList usedInfo;
 
     int decodedInfo = m_node->additionalInfo();
 
@@ -339,11 +361,11 @@ KFileItemDelegate::InformationList ViewProperties::additionalInfoV1() const
 
     foreach (const KFileItemDelegate::Information info, infoKeys) {
         if (decodedInfo & infoAccessor.bitValue(info)) {
-            usedInfos.append(info);
+            usedInfo.append(info);
         }
     }
 
-    return usedInfos;
+    return usedInfo;
 }
 
 KFileItemDelegate::InformationList ViewProperties::additionalInfoV2() const
@@ -355,8 +377,14 @@ KFileItemDelegate::InformationList ViewProperties::additionalInfoV2() const
     //
     // To get the representation as KFileItemDelegate::InformationList, the current
     // view-mode must be checked and the values of this mode added to the list.
+    //
+    // For the details-view a special case must be respected: Per default the size
+    // and date should be shown without creating a .directory file. Only if
+    // the user explictly has modified the properties of the details view (marked
+    // by "CustomizedDetails"), also a details-view with no additional information
+    // is accepted.
 
-    KFileItemDelegate::InformationList usedInfos;
+    KFileItemDelegate::InformationList usedInfo;
 
     // infoHash allows to get the mapped KFileItemDelegate::Information value
     // for a stored string-value in a fast way
@@ -378,11 +406,21 @@ KFileItemDelegate::InformationList ViewProperties::additionalInfoV2() const
         if (infoString.startsWith(prefix)) {
             const QString key = infoString.right(infoString.length() - prefixLength);
             Q_ASSERT(infoHash.contains(key));
-            usedInfos.append(infoHash.value(key));
+            usedInfo.append(infoHash.value(key));
         }
     }
 
-    return usedInfos;
+    // For the details view the size and date should be shown per default
+    // until the additional information has been explicitly changed by the user
+    const bool useDefaultValues = usedInfo.isEmpty()
+                                  && (m_node->viewMode() == DolphinView::DetailsView)
+                                  && !infoStringList.contains(CustomizedDetailsString);
+    if (useDefaultValues) {
+        usedInfo.append(KFileItemDelegate::Size);
+        usedInfo.append(KFileItemDelegate::ModificationTime);
+    }
+
+    return usedInfo;
 }
 
 QString ViewProperties::viewModePrefix() const
@@ -398,3 +436,16 @@ QString ViewProperties::viewModePrefix() const
 
     return prefix;
 }
+
+bool ViewProperties::isPartOfHome(const QString& filePath)
+{
+    // For performance reasons cache the path in a static QString
+    // (see QDir::homePath() for more details)
+    static QString homePath;
+    if (homePath.isEmpty()) {
+        homePath = QDir::homePath();
+        Q_ASSERT(!homePath.isEmpty());
+    }
+
+    return filePath.startsWith(homePath);
+}