changeset 63:2340b00561d2

* Add (but don't yet use) RecentFiles class to replace inline MRU logic
author Chris Cannam
date Wed, 17 Nov 2010 14:26:27 +0000
parents 68aebc316898
children 794db9353c7f
files common.h easyhg.pro mainwindow.cpp mainwindow.h recentfiles.cpp recentfiles.h settingsdialog.cpp settingsdialog.h
diffstat 8 files changed, 233 insertions(+), 67 deletions(-) [+]
line wrap: on
line diff
--- a/common.h	Wed Nov 17 13:32:56 2010 +0000
+++ b/common.h	Wed Nov 17 14:26:27 2010 +0000
@@ -41,8 +41,6 @@
 
 #define DEFAULT_HG_STAT_BITS (HGSTAT_M_BIT | HGSTAT_A_BIT | HGSTAT_R_BIT | HGSTAT_D_BIT | HGSTAT_U_BIT)
 
-#define NUM_PATHS_IN_MRU_LIST           5
-
 extern QString findExecutable(QString name);
 
 extern QString getSystem();
--- a/easyhg.pro	Wed Nov 17 13:32:56 2010 +0000
+++ b/easyhg.pro	Wed Nov 17 14:26:27 2010 +0000
@@ -22,7 +22,8 @@
     textabbrev.h \
     dateitem.h \
     colourset.h \
-    debug.h
+    debug.h \
+    recentfiles.h
 SOURCES = main.cpp \
     mainwindow.cpp \
     hgexpwidget.cpp \
@@ -39,7 +40,8 @@
     textabbrev.cpp \
     dateitem.cpp \
     colourset.cpp \
-    debug.cpp
+    debug.cpp \
+    recentfiles.cpp
 
 macx-* {
 SOURCES += common_osx.mm
--- a/mainwindow.cpp	Wed Nov 17 13:32:56 2010 +0000
+++ b/mainwindow.cpp	Wed Nov 17 14:26:27 2010 +0000
@@ -1454,17 +1454,6 @@
         workFolderPath = "";
     }
 
-    for(int i = 0; i < NUM_PATHS_IN_MRU_LIST; i++)
-    {
-        QString tmp;
-
-        tmp.sprintf("remoterepomrupath%d", i);
-        remoteRepoMruList[i] = settings.value(tmp, "").toString();
-
-        tmp.sprintf("workfoldermrupath%d", i);
-        workFolderMruList[i] = settings.value(tmp, "").toString();
-    }
-
     userInfo = settings.value("userinfo", "").toString();
 
     QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
@@ -1490,18 +1479,6 @@
     settings.setValue("size", size());
     settings.setValue("remoterepopath", remoteRepoPath);
     settings.setValue("workfolderpath", workFolderPath);
-
-    for(int i = 0; i < NUM_PATHS_IN_MRU_LIST; i++)
-    {
-        QString tmp;
-
-        tmp.sprintf("remoterepomrupath%d", i);
-        settings.setValue(tmp, remoteRepoMruList[i]);
-
-        tmp.sprintf("workfoldermrupath%d", i);
-        settings.setValue(tmp, workFolderMruList[i]);
-    }
-
     settings.setValue("userinfo", userInfo);
     settings.setValue("firststart", firstStart);
     settings.setValue("viewFileTypes", hgExp -> getFileTypesBits());
--- a/mainwindow.h	Wed Nov 17 13:32:56 2010 +0000
+++ b/mainwindow.h	Wed Nov 17 14:26:27 2010 +0000
@@ -76,9 +76,6 @@
     QString remoteRepoPath;
     QString workFolderPath;
 
-    QString remoteRepoMruList[NUM_PATHS_IN_MRU_LIST];
-    QString workFolderMruList[NUM_PATHS_IN_MRU_LIST];
-
     //User info for commits
     QString userInfo;
     bool        firstStart;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/recentfiles.cpp	Wed Nov 17 14:26:27 2010 +0000
@@ -0,0 +1,139 @@
+/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
+
+/*
+    EasyMercurial
+
+    Based on HgExplorer by Jari Korhonen
+    Copyright (c) 2010 Jari Korhonen
+    Copyright (c) 2010 Chris Cannam
+    Copyright (c) 2010 Queen Mary, University of London
+    
+    This program is free software; you can redistribute it and/or
+    modify it under the terms of the GNU General Public License as
+    published by the Free Software Foundation; either version 2 of the
+    License, or (at your option) any later version.  See the file
+    COPYING included with this distribution for more information.
+*/
+
+#include "recentfiles.h"
+
+#include <QFileInfo>
+#include <QSettings>
+#include <QRegExp>
+
+RecentFiles::RecentFiles(QString settingsGroup, size_t maxCount,
+                         bool ignoreTemporaries) :
+    m_settingsGroup(settingsGroup),
+    m_maxCount(maxCount),
+    m_ignoreTemporaries(ignoreTemporaries)
+{
+    read();
+}
+
+RecentFiles::~RecentFiles()
+{
+    // nothing
+}
+
+void
+RecentFiles::read()
+{
+    m_names.clear();
+    QSettings settings;
+    settings.beginGroup(m_settingsGroup);
+
+    for (size_t i = 0; i < 100; ++i) {
+        QString key = QString("recent-%1").arg(i);
+        QString name = settings.value(key, "").toString();
+        if (name == "") break;
+        if (i < m_maxCount) m_names.push_back(name);
+        else settings.setValue(key, "");
+    }
+
+    settings.endGroup();
+}
+
+void
+RecentFiles::write()
+{
+    QSettings settings;
+    settings.beginGroup(m_settingsGroup);
+
+    for (size_t i = 0; i < m_maxCount; ++i) {
+        QString key = QString("recent-%1").arg(i);
+        QString name = "";
+        if (i < m_names.size()) name = m_names[i];
+        settings.setValue(key, name);
+    }
+
+    settings.endGroup();
+}
+
+void
+RecentFiles::truncateAndWrite()
+{
+    while (m_names.size() > m_maxCount) {
+        m_names.pop_back();
+    }
+    write();
+}
+
+QStringList
+RecentFiles::getRecent() const
+{
+    QStringList names;
+    for (size_t i = 0; i < m_maxCount; ++i) {
+        if (i < m_names.size()) {
+            names.push_back(m_names[i]);
+        }
+    }
+    return names;
+}
+
+void
+RecentFiles::add(QString name)
+{
+    bool have = false;
+    for (size_t i = 0; i < m_names.size(); ++i) {
+        if (m_names[i] == name) {
+            have = true;
+            break;
+        }
+    }
+    
+    if (!have) {
+        m_names.push_front(name);
+    } else {
+        std::deque<QString> newnames;
+        newnames.push_back(name);
+        for (size_t i = 0; i < m_names.size(); ++i) {
+            if (m_names[i] == name) continue;
+            newnames.push_back(m_names[i]);
+        }
+        m_names = newnames;
+    }
+
+    truncateAndWrite();
+    emit recentChanged();
+}
+
+void
+RecentFiles::addFile(QString name)
+{
+    static QRegExp schemeRE("^[a-zA-Z]{2,5}://");
+    static QRegExp tempRE("[\\/][Tt]e?mp[\\/]");
+    if (schemeRE.indexIn(name) == 0) {
+        add(name);
+    } else {
+        QString absPath = QFileInfo(name).absoluteFilePath();
+        if (tempRE.indexIn(absPath) != -1) {
+            if (!m_ignoreTemporaries) {
+                add(absPath);
+            }
+        } else {
+            add(absPath);
+        }
+    }
+}
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/recentfiles.h	Wed Nov 17 14:26:27 2010 +0000
@@ -0,0 +1,84 @@
+/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
+
+/*
+    EasyMercurial
+
+    Based on HgExplorer by Jari Korhonen
+    Copyright (c) 2010 Jari Korhonen
+    Copyright (c) 2010 Chris Cannam
+    Copyright (c) 2010 Queen Mary, University of London
+    
+    This program is free software; you can redistribute it and/or
+    modify it under the terms of the GNU General Public License as
+    published by the Free Software Foundation; either version 2 of the
+    License, or (at your option) any later version.  See the file
+    COPYING included with this distribution for more information.
+*/
+
+#ifndef _RECENT_FILES_H_
+#define _RECENT_FILES_H_
+
+#include <QObject>
+#include <QString>
+#include <QStringList>
+#include <deque>
+
+/**
+ * RecentFiles manages a list of the names of recently-used objects,
+ * saving and restoring that list via QSettings.  The names do not
+ * actually have to refer to files.
+ */
+
+class RecentFiles : public QObject
+{
+    Q_OBJECT
+
+public:
+    /**
+     * Construct a RecentFiles object that saves and restores in the
+     * given QSettings group and truncates when the given count of
+     * strings is reached.
+     */
+    RecentFiles(QString settingsGroup = "RecentFiles",
+                size_t maxCount = 10,
+                bool ignoreTemporaries = true);
+    virtual ~RecentFiles();
+
+    QString getSettingsGroup() const { return m_settingsGroup; }
+
+    int getMaxCount() const { return m_maxCount; }
+
+    QStringList getRecent() const;
+
+    /**
+     * Add a name that should be treated as a literal string.
+     */
+    void add(QString name);
+    
+    /**
+     * Add a name that is known to be either a file path or a URL.  If
+     * it looks like a URL, add it literally; otherwise treat it as a
+     * file path and canonicalise it appropriately.  Also takes into
+     * account the user preference for whether to include temporary
+     * files in the recent files menu: the file will not be added if
+     * the preference is set and the file appears to be a temporary
+     * one.
+     */
+    void addFile(QString name);
+
+signals:
+    void recentChanged();
+
+protected:
+    QString m_settingsGroup;
+    size_t m_maxCount;
+    bool m_ignoreTemporaries;
+
+    std::deque<QString> m_names;
+
+    void read();
+    void write();
+    void truncateAndWrite();
+};
+
+#endif
--- a/settingsdialog.cpp	Wed Nov 17 13:32:56 2010 +0000
+++ b/settingsdialog.cpp	Wed Nov 17 14:26:27 2010 +0000
@@ -37,10 +37,11 @@
     remoteRepoLabel = new QLabel(tr("Remote repository path, e.g. http://192.168.1.10:8000/ or /home/mike/anotherrepo/ or c:\\anotherrepo\\"));
     remoteRepoCombo = new QComboBox();
     remoteRepoCombo -> insertItem(0, mainWnd->remoteRepoPath);
-    for(int i = 0; i < NUM_PATHS_IN_MRU_LIST; i++)
+/*!!!    for(int i = 0; i < NUM_PATHS_IN_MRU_LIST; i++)
     {
         remoteRepoCombo -> insertItem(i + 1, mainWnd -> remoteRepoMruList[i]);
     }
+*/
     remoteRepoCombo -> setEditable(true);
     remoteRepoLabel -> setBuddy(remoteRepoCombo);
     remoteRepoBrowseButton = new QPushButton(tr("Browse..."));
@@ -48,10 +49,11 @@
     workFolderLabel = new QLabel(tr("Local work folder path, e.g. /home/mike/work/ or c:\\mike\\work\\"));
     workFolderCombo = new QComboBox();
     workFolderCombo -> insertItem(0, mainWnd -> workFolderPath);
-    for(int i = 0; i < NUM_PATHS_IN_MRU_LIST; i++)
+/*!!!    for(int i = 0; i < NUM_PATHS_IN_MRU_LIST; i++)
     {
         workFolderCombo -> insertItem(i + 1, mainWnd -> workFolderMruList[i]);
     }
+*/
     workFolderCombo -> setEditable(true);
     workFolderLabel -> setBuddy(workFolderCombo);
     workFolderBrowseButton = new QPushButton(tr("Browse..."));
@@ -104,7 +106,7 @@
 
     if (mainWnd -> remoteRepoPath  != remoteRepoCombo-> currentText())
     {
-        insertPathToMruList(mainWnd -> remoteRepoPath, mainWnd -> remoteRepoMruList);
+//!!!        insertPathToMruList(mainWnd -> remoteRepoPath, mainWnd -> remoteRepoMruList);
         mainWnd -> remoteRepoPath = remoteRepoCombo-> currentText();
     }
 
@@ -116,7 +118,7 @@
 
     if (mainWnd -> workFolderPath != tmp)
     {
-        insertPathToMruList(mainWnd -> workFolderPath, mainWnd -> workFolderMruList);
+//!!!        insertPathToMruList(mainWnd -> workFolderPath, mainWnd -> workFolderMruList);
         mainWnd -> workFolderPath = tmp;
     }
 
@@ -154,38 +156,6 @@
     close();
 }
 
-
-void SettingsDialog::insertPathToMruList(QString path, QString mruList[])
-{
-    bool matchFound = false;
-
-    for(int i = 0; i < NUM_PATHS_IN_MRU_LIST; i++)
-    {
-        if (path == mruList[i])
-        {
-            matchFound = true;
-            break;
-        }
-    }
-
-    if (!matchFound)
-    {
-        for(int i = NUM_PATHS_IN_MRU_LIST - 2; i >= 0; i--)
-        {
-            if (i == 0)
-            {
-                mruList[1] = mruList[0];
-                mruList[0] = path;
-            }
-            else
-            {
-                mruList[i + 1] = mruList[i];
-            }
-        }
-    }
-}
-
-
 void SettingsDialog::browseDirAndSetCombo(QComboBox *combo)
 {
     QString dir;
--- a/settingsdialog.h	Wed Nov 17 13:32:56 2010 +0000
+++ b/settingsdialog.h	Wed Nov 17 14:26:27 2010 +0000
@@ -59,7 +59,6 @@
     MainWindow  *mainWnd;
 
     void browseDirAndSetCombo(QComboBox *combo);
-    void insertPathToMruList(QString newPath, QString mruList[]);
 };