~elopio/ubuntu-filemanager-app/clean_context_menu_tests

« back to all changes in this revision

Viewing changes to src/plugin/folderlistmodel/trash/qtrashutilinfo.cpp

  • Committer: Tarmac
  • Author(s): carlos-mazieri
  • Date: 2014-05-17 12:57:46 UTC
  • mfrom: (168.5.5 pre5-repo)
  • Revision ID: tarmac-20140517125746-9kn0nwbz3w3w8v7f
it is the last part of https://code.launchpad.net/~carlos-mazieri/ubuntu-filemanager-app/app-devel/+merge/216409 in fact the branches should be equals.

Approved by Carlos Jose Mazieri, Ubuntu Phone Apps Jenkins Bot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**************************************************************************
 
2
 *
 
3
 * Copyright 2014 Canonical Ltd.
 
4
 * Copyright 2014 Carlos J Mazieri <carlos.mazieri@gmail.com>
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU Lesser General Public License as published by
 
8
 * the Free Software Foundation; version 3.
 
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 Lesser General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU Lesser General Public License
 
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
 *
 
18
 * File: qtrashutilinfo.cpp
 
19
 * Date: 16/03/2014
 
20
 */
 
21
 
 
22
#include "qtrashutilinfo.h"
 
23
 
 
24
#include <QDir>
 
25
#include <QSettings>
 
26
#include <QDateTime>
 
27
 
 
28
QLatin1String filesDirString("files");
 
29
QLatin1String infoDirString("info");
 
30
 
 
31
void QTrashUtilInfo::clear()
 
32
{
 
33
    trashRoot.clear();
 
34
    filesDir.clear();
 
35
    absFile.clear();
 
36
    infoDir.clear();
 
37
    absInfo.clear();
 
38
    valid = false;
 
39
}
 
40
 
 
41
 
 
42
QString QTrashUtilInfo::filesTrashDir(const QString &trashDir)
 
43
{
 
44
   QString filesDir(trashDir + QDir::separator() + filesDirString);
 
45
   return filesDir;
 
46
}
 
47
 
 
48
 
 
49
QString QTrashUtilInfo::infoTrashDir(const QString &trashDir)
 
50
{
 
51
   QString infoDir(trashDir + QDir::separator() + infoDirString);
 
52
   return infoDir;
 
53
}
 
54
 
 
55
 
 
56
void QTrashUtilInfo::setInfoFromTrashItem(const QString &absTrashItem)
 
57
{
 
58
    valid = false;
 
59
    QFileInfo item(absTrashItem);
 
60
    if (item.absolutePath().endsWith(filesDirString))
 
61
    {
 
62
        QFileInfo filesUnderRoot(item.absolutePath());
 
63
        QTrashUtilInfo::setInfo(filesUnderRoot.absolutePath(), absTrashItem);
 
64
    }
 
65
    else
 
66
    {
 
67
        clear();
 
68
    }
 
69
}
 
70
 
 
71
 
 
72
void QTrashUtilInfo::setInfo(const QString& trashRootDir, const QString& filename)
 
73
{
 
74
    valid = !trashRootDir.isEmpty();
 
75
    if (valid)
 
76
    {
 
77
        QFileInfo f(filename);
 
78
        trashRoot  = trashRootDir;
 
79
        filesDir   = filesTrashDir(trashRootDir);
 
80
        absFile    = filesDir + QDir::separator() + f.fileName();
 
81
        infoDir    = infoTrashDir(trashRootDir) ;
 
82
        absInfo    = infoDir + QDir::separator() + f.fileName() +
 
83
                     QLatin1String(".trashinfo");
 
84
    }
 
85
    else
 
86
    {
 
87
        clear();
 
88
    }
 
89
}
 
90
 
 
91
 
 
92
bool QTrashUtilInfo::isValid()
 
93
{
 
94
    return valid;
 
95
}
 
96
 
 
97
 
 
98
bool QTrashUtilInfo::existsFile()
 
99
{
 
100
    return QFileInfo(absFile).exists();
 
101
}
 
102
 
 
103
 
 
104
bool QTrashUtilInfo::existsInfoFile()
 
105
{
 
106
    return QFileInfo(absInfo).exists();
 
107
}
 
108
 
 
109
 
 
110
QString QTrashUtilInfo::getOriginalPathName()
 
111
{
 
112
    QString path;
 
113
    if (isValid())
 
114
    {
 
115
        QSettings inff(absInfo, QSettings::IniFormat);
 
116
        inff.beginGroup(QLatin1String("Trash Info"));
 
117
        QFileInfo f (inff.value(QLatin1String("Path")).toString());
 
118
        //Path contains the full pathname
 
119
        path = f.absoluteFilePath();
 
120
    }
 
121
    return path;
 
122
}
 
123
 
 
124
 
 
125
bool QTrashUtilInfo::createTrashInfoFile(const QString& orignalPathname)
 
126
{
 
127
    bool ret = isValid();
 
128
    if (ret)
 
129
    {
 
130
        QByteArray content("[Trash Info]\nPath=");
 
131
        content += orignalPathname + QLatin1Char('\n');
 
132
        content += "DeletionDate=";
 
133
        content += QDateTime::currentDateTime().toString(Qt::ISODate) + QLatin1Char('\n');
 
134
        QFile f(absInfo);
 
135
        ret = f.open(QFile::WriteOnly | QFile::Truncate) &&
 
136
              f.write(content) == content.size();
 
137
        f.close();
 
138
    }
 
139
    return ret;
 
140
}
 
141
 
 
142
 
 
143
 
 
144
bool QTrashUtilInfo::removeTrashInfoFile()
 
145
{
 
146
    QFile infoFile(absInfo);
 
147
    bool ret = false;
 
148
    if (valid && infoFile.exists())
 
149
    {
 
150
        ret = infoFile.remove();
 
151
    }
 
152
    return ret;
 
153
}