On 02/14/2014 05:32 PM, Dirk Hohndel wrote:
That is a good idea, Joseph.
I have included a patch to hide failed parses from the recent
files list.

--
Joshua Wambua


>From a4ead61d68f58f1be8ee44aaf174dab3aa727552 Mon Sep 17 00:00:00 2001
From: Joshua Wambua <[email protected]>
Date: Mon, 17 Feb 2014 10:22:52 +0300
Subject: [PATCH] [PATCH] [PATCH] Hide Parse Fails from recent files menu

This patch will hide files that fail to parse from the recent files menu.

Signed-off-by: Joshua Wambua <[email protected]>
---
 qt-ui/mainwindow.cpp | 135 ++++++++++++++++++++++++++++++++++++++++++++++++---
 qt-ui/mainwindow.h   |   2 +
 2 files changed, 130 insertions(+), 7 deletions(-)

diff --git a/qt-ui/mainwindow.cpp b/qt-ui/mainwindow.cpp
index 42b07fb..822ccb0 100644
--- a/qt-ui/mainwindow.cpp
+++ b/qt-ui/mainwindow.cpp
@@ -709,18 +709,34 @@ void MainWindow::loadRecentFiles(QSettings *s)
 {
 	QStringList files;
 	bool modified = false;
+    QSettings sFailed;
+    QStringList failedFiles;
+
+    //Fetch all failed parses
+    sFailed.beginGroup("Failed_Parses");
+    for (int c = 1; c <= 10; c++) {
+        QString key = QString("File_%1").arg(c);
+        if (sFailed.contains(key)) {
+            QString file = sFailed.value(key).toString();
+            failedFiles.append(file);
+        } else {
+            break;
+        }
+    }
 
 	s->beginGroup("Recent_Files");
 	for (int c = 1; c <= 4; c++) {
 		QString key = QString("File_%1").arg(c);
 		if (s->contains(key)) {
 			QString file = s->value(key).toString();
-
-			if (QFile::exists(file)) {
-				files.append(file);
-			} else {
-				modified = true;
-			}
+            //Dont add to list if parse previously failed
+            if (!failedFiles.contains(file)) {
+                if (QFile::exists(file)) {
+                    files.append(file);
+                } else {
+                    modified = true;
+                }
+            }
 		} else {
 			break;
 		}
@@ -811,7 +827,103 @@ void MainWindow::addRecentFile(const QStringList &newFiles)
 	s.endGroup();
 	s.sync();
 
-	loadRecentFiles(&s);
+    loadRecentFiles(&s);
+}
+
+void MainWindow::addParseFailedFiles(const QStringList &newFiles)
+{
+    QStringList files; //List of all parse fails
+    QSettings s;
+
+    s.beginGroup("Failed_Parses");
+
+    //Load past parse fails
+    for (int c = 1; c <= 10; c++) {
+        QString key = QString("File_%1").arg(c);
+        if (s.contains(key)) {
+            QString file = s.value(key).toString();
+
+            files.append(file);
+        } else {
+            break;
+        }
+    }
+
+    //Compare with new files and remove duplicates
+    foreach(const QString & file, newFiles) {
+        int index = files.indexOf(file);
+
+        if (index >= 0) {
+            files.removeAt(index);
+        }
+    }
+
+    //Append new file(s) to parse fails
+    foreach(const QString & file, newFiles) {
+        if (QFile::exists(file)) {
+            files.prepend(file);
+        }
+    }
+
+    while (files.count() > 10) {
+        files.removeLast();
+    }
+
+    for (int c = 0; c < 4; c++) {
+        QString key = QString("File_%1").arg(c + 1);
+
+        if (files.count() > c) {
+            s.setValue(key, files.at(c));
+        } else {
+            if (s.contains(key)) {
+                s.remove(key);
+            }
+        }
+    }
+    s.endGroup();
+    s.sync();
+}
+
+void MainWindow::removeFilesFromParseFails(const QStringList &newFiles)
+{
+        QStringList files; //List of all parse fails
+        QSettings s;
+
+        s.beginGroup("Failed_Parses");
+
+        //Load past parse fails
+        for (int c = 1; c <= 10; c++) {
+            QString key = QString("File_%1").arg(c);
+            if (s.contains(key)) {
+                QString file = s.value(key).toString();
+
+                files.append(file);
+            } else {
+                break;
+            }
+        }
+        //Remove current list
+        foreach(const QString & file, newFiles) {
+            int index = files.indexOf(file);
+
+            if (index >= 0) {
+                files.removeAt(index);
+            }
+        }
+
+        for (int c = 0; c < 4; c++) {
+            QString key = QString("File_%1").arg(c + 1);
+
+            if (files.count() > c) {
+                s.setValue(key, files.at(c));
+            } else {
+                if (s.contains(key)) {
+                    s.remove(key);
+                }
+            }
+        }
+        s.endGroup();
+        s.sync();
 }
 
 void MainWindow::recentFileTriggered(bool checked)
@@ -931,6 +1043,10 @@ void MainWindow::loadFiles(const QStringList fileNames)
 	char *error = NULL;
 	QByteArray fileNamePtr;
 
+    QStringList failedFiles;
+    //Remove files from failed parses
+    removeFilesFromParseFails(fileNames);
+
 	for (int i = 0; i < fileNames.size(); ++i) {
 		fileNamePtr = QFile::encodeName(fileNames.at(i));
 		parse_file(fileNamePtr.data(), &error);
@@ -940,9 +1056,14 @@ void MainWindow::loadFiles(const QStringList fileNames)
 		if (error != NULL) {
 			showError(error);
 			free(error);
+            //Add this file to parse fails
+            failedFiles.append(fileNames.at(i));
 		}
 	}
 
+    if (failedFiles.size() > 0)
+            addParseFailedFiles(failedFiles);
+
 	process_dives(false, false);
 	addRecentFile(fileNames);
 
diff --git a/qt-ui/mainwindow.h b/qt-ui/mainwindow.h
index 24d3d70..2847140 100644
--- a/qt-ui/mainwindow.h
+++ b/qt-ui/mainwindow.h
@@ -47,6 +47,8 @@ public:
 	MainTab *information();
 	void loadRecentFiles(QSettings *s);
 	void addRecentFile(const QStringList &newFiles);
+    void addParseFailedFiles(const QStringList &newFiles);
+    void removeFilesFromParseFails(const QStringList &newFiles);
 	DiveListView *dive_list();
 	GlobeGPS *globe();
 	void showError(QString message);
-- 
1.8.5.5

_______________________________________________
subsurface mailing list
[email protected]
http://lists.hohndel.org/cgi-bin/mailman/listinfo/subsurface

Reply via email to