Commit 10e458d5 authored by Tobias Hunger's avatar Tobias Hunger

TaskList: Fix crash

Fix crash when a tasks-file with relative paths is reloaded after
its project was closed.

Change-Id: I3d0b6a3b459e391a3415d765941b2685e601b19f
Reviewed-by: default avatarTobias Hunger <tobias.hunger@digia.com>
parent 49d0789e
......@@ -38,8 +38,7 @@ using namespace TaskList::Internal;
// TaskFile
// --------------------------------------------------------------------------
TaskFile::TaskFile(QObject *parent) : Core::IDocument(parent),
m_context(0)
TaskFile::TaskFile(QObject *parent) : Core::IDocument(parent)
{ }
TaskFile::~TaskFile()
......@@ -101,16 +100,15 @@ bool TaskFile::reload(QString *errorString, ReloadFlag flag, ChangeType type)
bool TaskFile::open(QString *errorString, const QString &fileName)
{
setFilePath(fileName);
return TaskList::TaskListPlugin::loadFile(errorString, m_context, fileName);
return TaskList::TaskListPlugin::loadFile(errorString, m_baseDir, fileName);
}
ProjectExplorer::Project *TaskFile::context() const
QString TaskFile::baseDir() const
{
return m_context;
return m_baseDir;
}
void TaskFile::setContext(ProjectExplorer::Project *context)
void TaskFile::setBaseDir(const QString &base)
{
m_context = context;
m_baseDir = base;
}
......@@ -59,11 +59,11 @@ public:
bool open(QString *errorString, const QString &fileName);
ProjectExplorer::Project *context() const;
void setContext(ProjectExplorer::Project *context);
QString baseDir() const;
void setBaseDir(const QString &base);
private:
ProjectExplorer::Project *m_context;
QString m_baseDir;
};
} // namespace Internal
......
......@@ -32,6 +32,7 @@
#include "taskfile.h"
#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/project.h>
#include <coreplugin/icore.h>
#include <coreplugin/documentmanager.h>
......@@ -53,14 +54,14 @@ TaskFileFactory::TaskFileFactory(QObject * parent) :
Core::IDocument *TaskFileFactory::open(const QString &fileName)
{
ProjectExplorer::Project *context = ProjectExplorer::ProjectExplorerPlugin::currentProject();
return open(context, fileName);
ProjectExplorer::Project *project = ProjectExplorer::ProjectExplorerPlugin::currentProject();
return open(project ? project->projectDirectory() : QString(), fileName);
}
Core::IDocument *TaskFileFactory::open(ProjectExplorer::Project *context, const QString &fileName)
Core::IDocument *TaskFileFactory::open(const QString &base, const QString &fileName)
{
TaskFile *file = new TaskFile(this);
file->setContext(context);
file->setBaseDir(base);
QString errorString;
if (!file->open(&errorString, fileName)) {
......
......@@ -46,7 +46,7 @@ public:
TaskFileFactory(QObject *parent = 0);
Core::IDocument *open(const QString &fileName);
Core::IDocument *open(ProjectExplorer::Project *context, const QString &fileName);
Core::IDocument *open(const QString &base, const QString &fileName);
void closeAllFiles();
......
......@@ -50,7 +50,7 @@ using namespace ProjectExplorer;
using namespace TaskList::Internal;
static const char SESSION_FILE_KEY[] = "TaskList.File";
static const char SESSION_CONTEXT_KEY[] = "TaskList.Context";
static const char SESSION_BASE_KEY[] = "TaskList.BaseDir";
namespace TaskList {
......@@ -102,7 +102,7 @@ static QString unescape(const QString &input)
return result;
}
static bool parseTaskFile(QString *errorString, Project *context, const QString &name)
static bool parseTaskFile(QString *errorString, const QString &base, const QString &name)
{
QFile tf(name);
if (!tf.open(QIODevice::ReadOnly)) {
......@@ -142,8 +142,8 @@ static bool parseTaskFile(QString *errorString, Project *context, const QString
if (!file.isEmpty()) {
file = QDir::fromNativeSeparators(file);
QFileInfo fi(file);
if (fi.isRelative() && context) {
QString fullPath = context->projectDirectory() + QLatin1Char('/') + file;
if (fi.isRelative() && !base.isEmpty()) {
QString fullPath = base + QLatin1Char('/') + file;
fi.setFile(fullPath);
file = fi.absoluteFilePath();
}
......@@ -182,14 +182,13 @@ bool TaskListPlugin::initialize(const QStringList &arguments, QString *errorMess
return true;
}
bool TaskListPlugin::loadFile(QString *errorString, Project *context, const QString &fileName)
bool TaskListPlugin::loadFile(QString *errorString, const QString &context, const QString &fileName)
{
clearTasks();
bool result = parseTaskFile(errorString, context, fileName);
if (result) {
SessionManager::setValue(QLatin1String(SESSION_CONTEXT_KEY),
context ? context->projectFilePath() : QString());
SessionManager::setValue(QLatin1String(SESSION_BASE_KEY), context);
SessionManager::setValue(QLatin1String(SESSION_FILE_KEY), fileName);
} else {
stopMonitoring();
......@@ -200,7 +199,7 @@ bool TaskListPlugin::loadFile(QString *errorString, Project *context, const QStr
void TaskListPlugin::stopMonitoring()
{
SessionManager::setValue(QLatin1String(SESSION_CONTEXT_KEY), QString());
SessionManager::setValue(QLatin1String(SESSION_BASE_KEY), QString());
SessionManager::setValue(QLatin1String(SESSION_FILE_KEY), QString());
m_fileFactory->closeAllFiles();
......@@ -216,8 +215,7 @@ void TaskListPlugin::loadDataFromSession()
const QString fileName = SessionManager::value(QLatin1String(SESSION_FILE_KEY)).toString();
if (fileName.isEmpty())
return;
Project *project = SessionManager::projectForFile(SessionManager::value(QLatin1String(SESSION_CONTEXT_KEY)).toString());
m_fileFactory->open(project, fileName);
m_fileFactory->open(SessionManager::value(QLatin1String(SESSION_BASE_KEY)).toString(), fileName);
}
} // namespace TaskList
......
......@@ -45,7 +45,7 @@ public:
bool initialize(const QStringList &arguments, QString *errorMessage);
void extensionsInitialized() {}
static bool loadFile(QString *errorString, ProjectExplorer::Project *context, const QString &fileName);
static bool loadFile(QString *errorString, const QString &context, const QString &fileName);
static void stopMonitoring();
static void clearTasks();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment