[Pkg-owncloud-commits] [owncloud-client] 294/498: FolderMan::checkPathValidityForNewFolder

Sandro Knauß hefee-guest at moszumanska.debian.org
Tue Aug 11 14:49:00 UTC 2015


This is an automated email from the git hooks/post-receive script.

hefee-guest pushed a commit to branch master
in repository owncloud-client.

commit 4dbe0693df553b091c1fe4022d3d8154494cfb66
Author: Olivier Goffart <ogoffart at woboq.com>
Date:   Mon Jul 13 14:35:19 2015 +0200

    FolderMan::checkPathValidityForNewFolder
    
    factorize the function to check the validity of a new folder out of
    the folder wizard to the FolderMan.
    
    There is even a test for it now.
    
    That way we can also use it in the normal wizard
---
 src/gui/folderman.cpp    | 58 +++++++++++++++++++++++++++++++-
 src/gui/folderman.h      | 11 +++++-
 src/gui/folderwizard.cpp | 75 ++++-------------------------------------
 test/CMakeLists.txt      | 10 ++++++
 test/stub.cpp            |  4 +++
 test/testfolderman.h     | 87 ++++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 175 insertions(+), 70 deletions(-)

diff --git a/src/gui/folderman.cpp b/src/gui/folderman.cpp
index 552a9a7..546d00d 100644
--- a/src/gui/folderman.cpp
+++ b/src/gui/folderman.cpp
@@ -764,7 +764,7 @@ Folder* FolderMan::addFolder(AccountState* accountState, const FolderDefinition&
     }
 
     auto folder = addFolderInternal(folderDefinition);
-    if(folder) {
+    if(folder && accountState) {
         folder->setAccountState(accountState);
         folder->saveToSettings();
     }
@@ -1096,4 +1096,60 @@ QString FolderMan::statusToString( SyncResult syncStatus, bool paused ) const
     return folderMessage;
 }
 
+QString FolderMan::checkPathValidityForNewFolder(const QString& path)
+{
+    QFileInfo selFile( path );
+    QString userInput = selFile.canonicalFilePath();
+
+    QStringList warnStrings;
+
+    if( !selFile.isDir() ) {
+        return tr("No valid local folder selected!");
+    }
+
+    if ( !selFile.isWritable() ) {
+        return tr("You have no permission to write to the selected folder!");
+    }
+
+    // check if the local directory isn't used yet in another ownCloud sync
+
+    for (auto i = _folderMap.constBegin(); i != _folderMap.constEnd(); ++i ) {
+        Folder *f = static_cast<Folder*>(i.value());
+        QString folderDir = QDir( f->path() ).canonicalPath();
+        if( folderDir.isEmpty() ) {
+            continue;
+        }
+        if( ! folderDir.endsWith(QLatin1Char('/')) ) folderDir.append(QLatin1Char('/'));
+
+        if (QDir::cleanPath(f->path()) == QDir::cleanPath(userInput)
+                && QDir::cleanPath(QDir(f->path()).canonicalPath()) == QDir(userInput).canonicalPath()) {
+            return tr("The local path %1 is already an upload folder. Please pick another one!")
+                .arg(QDir::toNativeSeparators(userInput));
+        }
+        if (QDir::cleanPath(folderDir).startsWith(QDir::cleanPath(userInput)+'/')) {
+            return tr("An already configured folder is contained in the current entry.");
+        }
+
+        QString absCleanUserFolder = QDir::cleanPath(QDir(userInput).canonicalPath())+'/';
+        if (QDir::cleanPath(folderDir).startsWith(absCleanUserFolder) ) {
+            return tr("The selected folder is a symbolic link. An already configured "
+                      "folder is contained in the folder this link is pointing to.");
+        }
+
+        if (QDir::cleanPath(QString(userInput)).startsWith( QDir::cleanPath(folderDir)+'/')) {
+            return tr("An already configured folder contains the currently entered folder.");
+        }
+
+        if (absCleanUserFolder.startsWith( QDir::cleanPath(folderDir)+'/')) {
+            return tr("The selected folder is a symbolic link. An already configured folder "
+                      "is the parent of the current selected contains the folder this link is "
+                      "pointing to.");
+        }
+    }
+
+    return QString();
+
+}
+
+
 } // namespace OCC
diff --git a/src/gui/folderman.h b/src/gui/folderman.h
index e834427..f791773 100644
--- a/src/gui/folderman.h
+++ b/src/gui/folderman.h
@@ -26,6 +26,7 @@
 #include "syncfileitem.h"
 
 class QSignalMapper;
+class TestFolderMan;
 
 namespace OCC {
 
@@ -88,6 +89,14 @@ public:
 
     SocketApi *socketApi();
 
+    /**
+     * Check if @a path is a valid path for a new folder considering the already sync'ed items.
+     * Make sure that this folder, or any subfolder is not sync'ed alrady.
+     *
+     * @returns an empty string if it is allowed, or an error if it is not allowed
+     */
+    QString checkPathValidityForNewFolder(const QString &path);
+
 signals:
     /**
       * signal to indicate a folder has changed its sync state.
@@ -138,7 +147,6 @@ public slots:
     void slotAccountStateChanged();
 
 private slots:
-
     // slot to take the next folder from queue and start syncing.
     void slotStartScheduledFolderSync();
     void slotEtagPollTimerTimeout();
@@ -189,6 +197,7 @@ private:
     static FolderMan *_instance;
     explicit FolderMan(QObject *parent = 0);
     friend class OCC::Application;
+    friend class ::TestFolderMan;
 };
 
 } // namespace OCC
diff --git a/src/gui/folderwizard.cpp b/src/gui/folderwizard.cpp
index 4382a37..e7a4458 100644
--- a/src/gui/folderwizard.cpp
+++ b/src/gui/folderwizard.cpp
@@ -92,76 +92,14 @@ void FolderWizardLocalPath::cleanupPage()
 
 bool FolderWizardLocalPath::isComplete() const
 {
-  QFileInfo selFile( QDir::fromNativeSeparators(_ui.localFolderLineEdit->text()) );
-  QString   userInput = selFile.canonicalFilePath();
+    QString errorStr = FolderMan::instance()->checkPathValidityForNewFolder(
+        QDir::fromNativeSeparators(_ui.localFolderLineEdit->text()));
 
-  QStringList warnStrings;
-
-  bool isOk = selFile.isDir();
-  if( !isOk ) {
-    warnStrings.append(tr("No valid local folder selected!"));
-  }
-
-  if (isOk && !selFile.isWritable()) {
-      isOk = false;
-      warnStrings.append(tr("You have no permission to write to the selected folder!"));
-  }
-
-  // check if the local directory isn't used yet in another ownCloud sync
-  Folder::Map map = FolderMan::instance()->map();
-
-  if( isOk ) {
-    Folder::Map::const_iterator i = map.constBegin();
-    while( isOk && i != map.constEnd() ) {
-      Folder *f = static_cast<Folder*>(i.value());
-      QString folderDir = QDir( f->path() ).canonicalPath();
-      if( folderDir.isEmpty() )
-      {
-        isOk = true;
-        qDebug() << "Absolute path for folder: " << f->path() << " doesn't exist. Skipping.";
-        i++;
-        continue;
-      }
-      if( ! folderDir.endsWith(QLatin1Char('/')) ) folderDir.append(QLatin1Char('/'));
-
-      qDebug() << "Checking local path: " << folderDir << " <-> " << userInput;
-      if( QDir::cleanPath(f->path())  == QDir::cleanPath(userInput)  &&
-              QDir::cleanPath(QDir(f->path()).canonicalPath()) == QDir(userInput).canonicalPath() ) {
-        isOk = false;
-        warnStrings.append( tr("The local path %1 is already an upload folder. Please pick another one!")
-                           .arg(QDir::toNativeSeparators(userInput)) );
-      }
-      if( isOk && QDir::cleanPath(folderDir).startsWith(QDir::cleanPath(userInput)+'/') ) {
-        qDebug() << "A already configured folder is child of the current selected";
-        warnStrings.append( tr("An already configured folder is contained in the current entry."));
-        isOk = false;
-      }
-
-      QString absCleanUserFolder = QDir::cleanPath(QDir(userInput).canonicalPath())+'/';
-      if( isOk && QDir::cleanPath(folderDir).startsWith(absCleanUserFolder) ) {
-          qDebug() << "A already configured folder is child of the current selected";
-          warnStrings.append( tr("The selected folder is a symbolic link. An already configured "
-                                "folder is contained in the folder this link is pointing to."));
-          isOk = false;
-      }
-
-      if( isOk && QDir::cleanPath(QString(userInput)).startsWith( QDir::cleanPath(folderDir)+'/') ) {
-        qDebug() << "An already configured folder is parent of the current selected";
-        warnStrings.append( tr("An already configured folder contains the currently entered folder."));
-        isOk = false;
-      }
-      if( isOk && absCleanUserFolder.startsWith( QDir::cleanPath(folderDir)+'/') ) {
-          qDebug() << "The selected folder is a symbolic link. An already configured folder is\n"
-                      "the parent of the current selected contains the folder this link is pointing to.";
-          warnStrings.append( tr("The selected folder is a symbolic link. An already configured folder "
-                                "is the parent of the current selected contains the folder this link is "
-                                "pointing to."));
-          isOk = false;
-      }
-
-      i++;
+    bool isOk = errorStr.isEmpty();
+    QStringList warnStrings;
+    if (!isOk) {
+        warnStrings << errorStr;
     }
-  }
 
   // check if the alias is unique.
   QString alias = _ui.aliasLineEdit->text();
@@ -170,6 +108,7 @@ bool FolderWizardLocalPath::isComplete() const
     isOk = false;
   }
 
+  auto map = FolderMan::instance()->map();
   Folder::Map::const_iterator i = map.constBegin();
   bool goon = true;
   while( goon && i != map.constEnd() ) {
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index ef64f5d..b92f3a6 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -36,3 +36,13 @@ owncloud_add_test(XmlParse "")
 owncloud_add_test(FileSystem "")
 owncloud_add_test(TransChecksumValidator "")
 
+
+SET(FolderMan_SRC ../src/gui/folderman.cpp)
+list(APPEND FolderMan_SRC ../src/gui/folder.cpp )
+list(APPEND FolderMan_SRC ../src/gui/socketapi.cpp )
+list(APPEND FolderMan_SRC ../src/gui/accountstate.cpp )
+list(APPEND FolderMan_SRC ../src/gui/syncrunfilelog.cpp )
+list(APPEND FolderMan_SRC ${FolderWatcher_SRC})
+list(APPEND FolderMan_SRC stub.cpp )
+owncloud_add_test(FolderMan "${FolderMan_SRC}")
+
diff --git a/test/stub.cpp b/test/stub.cpp
new file mode 100644
index 0000000..8241b2c
--- /dev/null
+++ b/test/stub.cpp
@@ -0,0 +1,4 @@
+// stub to prevent linker error
+#include "accountmanager.h"
+OCC::AccountManager *OCC::AccountManager::instance() { return 0; }
+
diff --git a/test/testfolderman.h b/test/testfolderman.h
new file mode 100644
index 0000000..d6cfac2
--- /dev/null
+++ b/test/testfolderman.h
@@ -0,0 +1,87 @@
+/*
+ *    This software is in the public domain, furnished "as is", without technical
+ *    support, and with no warranty, express or implied, as to its usefulness for
+ *    any purpose.
+ *
+ */
+
+#pragma once
+
+#include <QTemporaryDir>
+#include <QtTest>
+
+#include "utility.h"
+#include "folderman.h"
+#include "accountstate.h"
+
+using namespace OCC;
+
+
+static FolderDefinition folderDefinition(const QString &path) {
+    FolderDefinition d;
+    d.localPath = path;
+    d.targetPath = path;
+    d.alias = path;
+    return d;
+}
+
+
+class TestFolderMan: public QObject
+{
+    Q_OBJECT
+
+    FolderMan _fm;
+
+private slots:
+    void testCheckPathValidityForNewFolder()
+    {
+        QTemporaryDir dir;
+        QVERIFY(dir.isValid());
+        QDir dir2(dir.path());
+        QVERIFY(dir2.mkpath("sub/ownCloud1/folder/f"));
+        QVERIFY(dir2.mkpath("ownCloud2"));
+        QVERIFY(dir2.mkpath("sub/free"));
+        QVERIFY(dir2.mkpath("free2/sub"));
+
+        FolderMan *folderman = FolderMan::instance();
+        QCOMPARE(folderman, &_fm);
+        QVERIFY(folderman->addFolder(0, folderDefinition(dir.path() + "/sub/ownCloud1")));
+        QVERIFY(folderman->addFolder(0, folderDefinition(dir.path() + "/ownCloud2")));
+
+
+        // those should be allowed
+        QVERIFY(folderman->checkPathValidityForNewFolder(dir.path() + "/sub/free").isNull());
+        QVERIFY(folderman->checkPathValidityForNewFolder(dir.path() + "/free2/").isNull());
+
+        // Not an existing directory -> Error
+        QVERIFY(!folderman->checkPathValidityForNewFolder(dir.path() + "/sub/bliblablu").isNull());
+
+        // There are folders configured in those folders: -> ERROR
+        QVERIFY(!folderman->checkPathValidityForNewFolder(dir.path() + "/sub/ownCloud1").isNull());
+        QVERIFY(!folderman->checkPathValidityForNewFolder(dir.path() + "/ownCloud2/").isNull());
+        QVERIFY(!folderman->checkPathValidityForNewFolder(dir.path() + "/sub").isNull());
+        QVERIFY(!folderman->checkPathValidityForNewFolder(dir.path() + "/sub/").isNull());
+        QVERIFY(!folderman->checkPathValidityForNewFolder(dir.path()).isNull());
+        QVERIFY(!folderman->checkPathValidityForNewFolder(dir.path() + "/sub/ownCloud1/folder").isNull());
+        QVERIFY(!folderman->checkPathValidityForNewFolder(dir.path() + "/sub/ownCloud1/folder/f").isNull());
+
+
+        // make a bunch of links
+        QVERIFY(QFile::link(dir.path() + "/sub/free", dir.path() + "/link1"));
+        QVERIFY(QFile::link(dir.path() + "/sub", dir.path() + "/link2"));
+        QVERIFY(QFile::link(dir.path() + "/sub/ownCloud1", dir.path() + "/link3"));
+        QVERIFY(QFile::link(dir.path() + "/sub/ownCloud1/folder", dir.path() + "/link4"));
+
+        // Ok
+        QVERIFY(folderman->checkPathValidityForNewFolder(dir.path() + "/link1").isNull());
+        QVERIFY(folderman->checkPathValidityForNewFolder(dir.path() + "/link2/free").isNull());
+
+        // Not Ok
+        QVERIFY(!folderman->checkPathValidityForNewFolder(dir.path() + "/link2").isNull());
+        QVERIFY(!folderman->checkPathValidityForNewFolder(dir.path() + "/link3").isNull());
+        QVERIFY(!folderman->checkPathValidityForNewFolder(dir.path() + "/link4").isNull());
+        QVERIFY(!folderman->checkPathValidityForNewFolder(dir.path() + "/link3/folder").isNull());
+    }
+};
+
+

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/owncloud-client.git



More information about the Pkg-owncloud-commits mailing list