~ubuntu-branches/ubuntu/wily/psi/wily

« back to all changes in this revision

Viewing changes to src/fileutil.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2008-08-28 18:46:52 UTC
  • mfrom: (1.2.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20080828184652-iiik12dl91nq7cdi
Tags: 0.12-2
Uploading to unstable (Closes: Bug#494352)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * fileutil.h - common file dialogs
 
3
 * Copyright (C) 2008  Michail Pishchagin
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or
 
6
 * modify it under the terms of the GNU General Public License
 
7
 * as published by the Free Software Foundation; either version 2
 
8
 * of the License, or (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this library; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
 *
 
19
 */
 
20
 
 
21
#include "fileutil.h"
 
22
 
 
23
#include <QFileInfo>
 
24
#include <QDir>
 
25
#include <QFileDialog>
 
26
#include <QMessageBox>
 
27
 
 
28
#include "psioptions.h"
 
29
 
 
30
static QString lastUsedOpenPathOptionPath = "options.ui.last-used-open-path";
 
31
static QString lastUsedSavePathOptionPath = "options.ui.last-used-save-path";
 
32
 
 
33
QString FileUtil::lastUsedOpenPath()
 
34
{
 
35
        return PsiOptions::instance()->getOption(lastUsedOpenPathOptionPath).toString();
 
36
}
 
37
 
 
38
void FileUtil::setLastUsedOpenPath(const QString& path)
 
39
{
 
40
        QFileInfo fi(path);
 
41
        if (fi.exists()) {
 
42
                PsiOptions::instance()->setOption(lastUsedOpenPathOptionPath, path);
 
43
        }
 
44
}
 
45
 
 
46
QString FileUtil::lastUsedSavePath()
 
47
{
 
48
        return PsiOptions::instance()->getOption(lastUsedSavePathOptionPath).toString();
 
49
}
 
50
 
 
51
void FileUtil::setLastUsedSavePath(const QString& path)
 
52
{
 
53
        QFileInfo fi(path);
 
54
        if (fi.exists()) {
 
55
                PsiOptions::instance()->setOption(lastUsedSavePathOptionPath, path);
 
56
        }
 
57
}
 
58
 
 
59
QString FileUtil::getOpenFileName(QWidget* parent, const QString& caption, const QString& filter, QString* selectedFilter)
 
60
{
 
61
        while (1) {
 
62
                if (lastUsedOpenPath().isEmpty()) {
 
63
                        setLastUsedOpenPath(QDir::homeDirPath());
 
64
                }
 
65
                QString fileName = QFileDialog::getOpenFileName(parent, caption, lastUsedOpenPath(), filter, selectedFilter);
 
66
                if (!fileName.isEmpty()) {
 
67
                        QFileInfo fi(fileName);
 
68
                        if (!fi.exists()) {
 
69
                                QMessageBox::information(parent, tr("Error"), tr("The file specified does not exist."));
 
70
                                continue;
 
71
                        }
 
72
 
 
73
                        setLastUsedOpenPath(fi.dirPath());
 
74
                        return fileName;
 
75
                }
 
76
                break;
 
77
        }
 
78
 
 
79
        return QString();
 
80
}
 
81
 
 
82
QString FileUtil::getSaveFileName(QWidget* parent, const QString& caption, const QString& defaultFileName, const QString& filter, QString* selectedFilter)
 
83
{
 
84
        if (lastUsedSavePath().isEmpty()) {
 
85
                if (!lastUsedOpenPath().isEmpty()) {
 
86
                        setLastUsedSavePath(lastUsedOpenPath());
 
87
                }
 
88
                else {
 
89
                        setLastUsedSavePath(QDir::homeDirPath());
 
90
                }
 
91
        }
 
92
 
 
93
        QString dir = QDir(lastUsedSavePath()).filePath(defaultFileName);
 
94
        QString fileName = QFileDialog::getSaveFileName(parent, caption, dir, filter, selectedFilter);
 
95
        if (!fileName.isEmpty()) {
 
96
                QFileInfo fi(fileName);
 
97
                if (QDir(fi.dirPath()).exists()) {
 
98
                        setLastUsedSavePath(fi.dirPath());
 
99
                        return fileName;
 
100
                }
 
101
        }
 
102
 
 
103
        return QString();
 
104
}
 
105
 
 
106
QString FileUtil::getImageFileName(QWidget* parent)
 
107
{
 
108
        return FileUtil::getOpenFileName(parent, tr("Choose a file"),
 
109
                                         tr("Images (*.png *.xpm *.jpg *.PNG *.XPM *.JPG)"));
 
110
}