Remote folder wizard: Add optional manual entry #2613
authorChristian Kamm <kamm@incasoftware.de>
Thu, 12 Mar 2015 14:59:59 +0000 (15:59 +0100)
committerChristian Kamm <kamm@incasoftware.de>
Fri, 13 Mar 2015 08:52:21 +0000 (09:52 +0100)
src/gui/folderwizard.cpp
src/gui/folderwizard.h
src/gui/folderwizardtargetpage.ui

index d676084895dda35cd5fbb1dd02b1544678488ebe..6326194cd3523bc1d982fd0e08f40d6bd29e56a1 100644 (file)
@@ -239,10 +239,13 @@ FolderWizardRemotePath::FolderWizardRemotePath(AccountPtr account)
 
     connect(_ui.addFolderButton, SIGNAL(clicked()), SLOT(slotAddRemoteFolder()));
     connect(_ui.refreshButton, SIGNAL(clicked()), SLOT(slotRefreshFolders()));
-    connect(_ui.folderTreeWidget, SIGNAL(itemClicked(QTreeWidgetItem*,int)), SIGNAL(completeChanged()));
-    connect(_ui.folderTreeWidget, SIGNAL(itemActivated(QTreeWidgetItem*,int)), SIGNAL(completeChanged()));
     connect(_ui.folderTreeWidget, SIGNAL(itemExpanded(QTreeWidgetItem*)), SLOT(slotItemExpanded(QTreeWidgetItem*)));
+    connect(_ui.folderTreeWidget, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)), SLOT(slotCurrentItemChanged(QTreeWidgetItem*)));
+    connect(_ui.folderEntry, SIGNAL(textEdited(QString)), SLOT(slotFolderEntryEdited(QString)));
 
+    _lscolTimer.setInterval(500);
+    _lscolTimer.setSingleShot(true);
+    connect(&_lscolTimer, SIGNAL(timeout()), SLOT(slotLsColFolderEntry()));
 }
 
 void FolderWizardRemotePath::slotAddRemoteFolder()
@@ -315,27 +318,58 @@ static QTreeWidgetItem* findFirstChild(QTreeWidgetItem *parent, const QString& t
 
 void FolderWizardRemotePath::recursiveInsert(QTreeWidgetItem *parent, QStringList pathTrail, QString path)
 {
-    QFileIconProvider prov;
-    QIcon folderIcon = prov.icon(QFileIconProvider::Folder);
-    if (pathTrail.size() == 0) {        
-        if (path.endsWith('/')) {
-            path.chop(1);
-        }
-        parent->setToolTip(0, path);
-        parent->setData(0, Qt::UserRole, path);
+    if (pathTrail.isEmpty())
+        return;
+
+    const QString parentPath = parent->data(0, Qt::UserRole).toString();
+    const QString folderName = pathTrail.first();
+    QString folderPath;
+    if (parentPath == QLatin1String("/")) {
+        folderPath = folderName;
     } else {
-        QTreeWidgetItem *item = findFirstChild(parent, pathTrail.first());
-        if (!item) {
-            item = new QTreeWidgetItem(parent);
-            item->setIcon(0, folderIcon);
-            item->setText(0, pathTrail.first());
-            item->setData(0, Qt::UserRole, pathTrail.first());
-            item->setChildIndicatorPolicy(QTreeWidgetItem::ShowIndicator);
-        }
+        folderPath = parentPath + "/" + folderName;
+    }
+    QTreeWidgetItem *item = findFirstChild(parent, folderName);
+    if (!item) {
+        item = new QTreeWidgetItem(parent);
+        QFileIconProvider prov;
+        QIcon folderIcon = prov.icon(QFileIconProvider::Folder);
+        item->setIcon(0, folderIcon);
+        item->setText(0, folderName);
+        item->setData(0, Qt::UserRole, folderPath);
+        item->setToolTip(0, folderPath);
+        item->setChildIndicatorPolicy(QTreeWidgetItem::ShowIndicator);
+    }
+
+    pathTrail.removeFirst();
+    recursiveInsert(item, pathTrail, path);
+}
 
-        pathTrail.removeFirst();
-        recursiveInsert(item, pathTrail, path);
+bool FolderWizardRemotePath::selectByPath(QString path)
+{
+    if (path.startsWith(QLatin1Char('/'))) {
+        path = path.mid(1);
+    }
+    if (path.endsWith(QLatin1Char('/'))) {
+        path.chop(1);
     }
+
+    QTreeWidgetItem *it = _ui.folderTreeWidget->topLevelItem(0);
+    if (!path.isEmpty()) {
+        const QStringList pathTrail = path.split(QLatin1Char('/'));
+        foreach (const QString& path, pathTrail) {
+            if (!it) {
+                return false;
+            }
+            it = findFirstChild(it, path);
+        }
+    }
+    if (!it) {
+        return false;
+    }
+
+    _ui.folderTreeWidget->setCurrentItem(it);
+    return true;
 }
 
 void FolderWizardRemotePath::slotUpdateDirectories(const QStringList &list)
@@ -367,6 +401,7 @@ void FolderWizardRemotePath::slotRefreshFolders()
             SLOT(slotUpdateDirectories(QStringList)));
     job->start();
     _ui.folderTreeWidget->clear();
+    _ui.folderEntry->clear();
 }
 
 void FolderWizardRemotePath::slotItemExpanded(QTreeWidgetItem *item)
@@ -379,6 +414,49 @@ void FolderWizardRemotePath::slotItemExpanded(QTreeWidgetItem *item)
     job->start();
 }
 
+void FolderWizardRemotePath::slotCurrentItemChanged(QTreeWidgetItem *item)
+{
+    if (item) {
+        QString dir = item->data(0, Qt::UserRole).toString();
+        if (!dir.startsWith(QLatin1Char('/'))) {
+            dir.prepend(QLatin1Char('/'));
+        }
+        _ui.folderEntry->setText(dir);
+    }
+
+    emit completeChanged();
+}
+
+void FolderWizardRemotePath::slotFolderEntryEdited(const QString& text)
+{
+    if (selectByPath(text)) {
+        _lscolTimer.stop();
+        return;
+    }
+
+    _ui.folderTreeWidget->setCurrentItem(0);
+    _lscolTimer.start(); // avoid sending a request on each keystroke
+}
+
+void FolderWizardRemotePath::slotLsColFolderEntry()
+{
+    QString path = _ui.folderEntry->text();
+    if (path.startsWith(QLatin1Char('/')))
+        path = path.mid(1);
+
+    LsColJob *job = new LsColJob(_account, path, this);
+    job->setProperties(QList<QByteArray>() << "resourcetype");
+    connect(job, SIGNAL(directoryListingSubfolders(QStringList)),
+            SLOT(slotTypedPathFound(QStringList)));
+    job->start();
+}
+
+void FolderWizardRemotePath::slotTypedPathFound(const QStringList& subpaths)
+{
+    slotUpdateDirectories(subpaths);
+    selectByPath(_ui.folderEntry->text());
+}
+
 FolderWizardRemotePath::~FolderWizardRemotePath()
 {
 }
index 88b86409b92e8cb5ee74ac51c99f931f749d9036..06a3476ba769c5e59d1c7fb270a67880b4777897 100644 (file)
@@ -86,11 +86,17 @@ protected slots:
     void slotUpdateDirectories(const QStringList&);
     void slotRefreshFolders();
     void slotItemExpanded(QTreeWidgetItem*);
+    void slotCurrentItemChanged(QTreeWidgetItem*);
+    void slotFolderEntryEdited(const QString& text);
+    void slotLsColFolderEntry();
+    void slotTypedPathFound(const QStringList& subpaths);
 private:
     void recursiveInsert(QTreeWidgetItem *parent, QStringList pathTrail, QString path);
+    bool selectByPath(QString path);
     Ui_FolderWizardTargetPage _ui;
     bool _warnWasVisible;
     AccountPtr _account;
+    QTimer _lscolTimer;
 };
 
 
index 0f242e1a725fd9822da6188e7609fe3d2ab1ddfa..97850ee641a7587639bc6d4655a8003d607f98a6 100644 (file)
    <string>Form</string>
   </property>
   <layout class="QGridLayout" name="gridLayout_6">
-   <item row="3" column="0">
+   <item row="1" column="0">
+    <widget class="QLineEdit" name="folderEntry"/>
+   </item>
+   <item row="2" column="0">
     <widget class="QFrame" name="warnFrame">
      <property name="palette">
       <palette>
      </layout>
     </widget>
    </item>
-   <item row="4" column="0">
+   <item row="3" column="0">
     <spacer name="verticalSpacer">
      <property name="orientation">
       <enum>Qt::Vertical</enum>