From d07773e737e781bea89149e076ca4c11b9bd6c1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20E=2E=20Narv=C3=A1ez?= Date: Fri, 20 Jan 2012 08:35:37 -0500 Subject: Refactoring Code to Load Configuration Document Factor out all code that loads the DOM Document, and use proper calls in each method. REVIEW: 103722 --- src/sessionmanager.cpp | 114 +++++++++++++++---------------------------------- 1 file changed, 34 insertions(+), 80 deletions(-) (limited to 'src') diff --git a/src/sessionmanager.cpp b/src/sessionmanager.cpp index e67012a9..d5298ecb 100644 --- a/src/sessionmanager.cpp +++ b/src/sessionmanager.cpp @@ -43,6 +43,28 @@ // Qt Includes #include +// Only used internally +bool readSessionDocument(QDomDocument & document, const QString & m_sessionFilePath) +{ + QFile sessionFile(m_sessionFilePath); + + if (!sessionFile.exists()) + return false; + + if (!sessionFile.open(QFile::ReadOnly)) + { + kDebug() << "Unable to open session file" << sessionFile.fileName(); + return false; + } + + if (!document.setContent(&sessionFile, false)) + { + kDebug() << "Unable to parse session file" << sessionFile.fileName(); + return false; + } + + return true; +} SessionManager::SessionManager(QObject *parent) : QObject(parent) @@ -120,21 +142,10 @@ void SessionManager::saveSession() bool SessionManager::restoreSessionFromScratch() { - QFile sessionFile(m_sessionFilePath); - if (!sessionFile.exists()) - return false; - if (!sessionFile.open(QFile::ReadOnly)) - { - kDebug() << "Unable to open session file" << sessionFile.fileName(); - return false; - } - QDomDocument document("session"); - if (!document.setContent(&sessionFile, false)) - { - kDebug() << "Unable to parse session file" << sessionFile.fileName(); - return false; - } + + if(!readSessionDocument(document, m_sessionFilePath)) + return false; for (unsigned int winNo = 0; winNo < document.elementsByTagName("window").length(); winNo++) { @@ -172,25 +183,10 @@ bool SessionManager::restoreSessionFromScratch() void SessionManager::restoreCrashedSession() { - QFile sessionFile(m_sessionFilePath); - if (!sessionFile.exists()) - { - kDebug() << "Unable to find session file" << sessionFile.fileName(); - return; - } - - if (!sessionFile.open(QFile::ReadOnly)) - { - kDebug() << "Unable to open session file" << sessionFile.fileName(); - return; - } - QDomDocument document("session"); - if (!document.setContent(&sessionFile, false)) - { - kDebug() << "Unable to parse session file" << sessionFile.fileName(); + + if (!readSessionDocument(document, m_sessionFilePath)) return; - } for (unsigned int winNo = 0; winNo < document.elementsByTagName("window").length(); winNo++) { @@ -227,25 +223,10 @@ void SessionManager::restoreCrashedSession() int SessionManager::restoreSavedSession() { - QFile sessionFile(m_sessionFilePath); - if (!sessionFile.exists()) - { - kDebug() << "Unable to find session file" << sessionFile.fileName(); - return 0; - } - - if (!sessionFile.open(QFile::ReadOnly)) - { - kDebug() << "Unable to open session file" << sessionFile.fileName(); - return 0; - } - QDomDocument document("session"); - if (!document.setContent(&sessionFile, false)) - { - kDebug() << "Unable to parse session file" << sessionFile.fileName(); + + if (!readSessionDocument(document, m_sessionFilePath)) return 0; - } unsigned int winNo; @@ -283,25 +264,10 @@ int SessionManager::restoreSavedSession() bool SessionManager::restoreMainWindow(MainWindow* window) { - QFile sessionFile(m_sessionFilePath); - if (!sessionFile.exists()) - { - kDebug() << "Unable to find session file" << sessionFile.fileName(); - return 0; - } - - if (!sessionFile.open(QFile::ReadOnly)) - { - kDebug() << "Unable to open session file" << sessionFile.fileName(); - return 0; - } - QDomDocument document("session"); - if (!document.setContent(&sessionFile, false)) - { - kDebug() << "Unable to parse session file" << sessionFile.fileName(); - return 0; - } + + if (!readSessionDocument(document, m_sessionFilePath)) + return false; unsigned int winNo; @@ -346,22 +312,10 @@ bool SessionManager::restoreMainWindow(MainWindow* window) QList SessionManager::closedSites() { QList list; - - QFile sessionFile(m_sessionFilePath); - if (!sessionFile.exists()) - return list; - if (!sessionFile.open(QFile::ReadOnly)) - { - kDebug() << "Unable to open session file" << sessionFile.fileName(); - return list; - } - QDomDocument document("session"); - if (!document.setContent(&sessionFile, false)) - { - kDebug() << "Unable to parse session file" << sessionFile.fileName(); + + if (!readSessionDocument(document, m_sessionFilePath)) return list; - } for (unsigned int tabNo = 0; tabNo < document.elementsByTagName("tab").length(); tabNo++) { -- cgit v1.2.1