1
/**************************************************************************
3
* Copyright 2014 Canonical Ltd.
4
* Copyright 2014 Carlos J Mazieri <carlos.mazieri@gmail.com>
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.
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.
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/>.
18
* File: trashiteminfo.cpp
22
#include "trashiteminfo.h"
23
#include "qtrashutilinfo.h"
24
#include "locationurl.h"
28
* \brief TrashItemInfo::TrashItemInfo() This constructor does not receive the Trash path
30
* \param urlPath the full pathname starting with th \a trashPath as "/home/devubuntu/.local/share/Trash/files/test.txt"
32
TrashItemInfo::TrashItemInfo(const QString &urlPath)
35
d_ptr->_path = urlPath;
36
d_ptr->_isLocal = true;
37
d_ptr->_normalizedPath = urlPath;
38
if (urlPath == LocationUrl::TrashRootURL)
44
QTrashUtilInfo trashInfo;
45
trashInfo.setInfoFromTrashItem(urlPath);
46
//try to guess the Trash path
47
if (trashInfo.isValid() && !trashInfo.filesDir.isEmpty())
50
init(trashInfo.filesDir);
52
QFileInfo maybeDiskUrl(urlPath);
53
if (maybeDiskUrl.exists())
55
d_ptr->setFileInfo(maybeDiskUrl);
61
TrashItemInfo::TrashItemInfo(const TrashItemInfo &other)
68
* \brief TrashItemInfo::TrashItemInfo()
70
* \param trashPath the trash PATH finished with "files" like as "/home/user/.local/share/Trash/files"
71
* \param urlPath the full pathname starting with th \a trashPath as "/home/user/.local/share/Trash/files/test.txt"
73
TrashItemInfo::TrashItemInfo(const QString& trashPath, const QString &urlPath)
74
: DirItemInfo(urlPath)
80
void TrashItemInfo::setRoot()
82
d_ptr->_isValid = true;
83
d_ptr->_isRoot = true;
85
d_ptr->_isReadable = true;
86
d_ptr->_isExecutable = true;
87
d_ptr->_exists = true;
88
d_ptr->_fileName.clear();
92
void TrashItemInfo::init(const QString& trashPath)
94
if (trashPath == absoluteFilePath())
96
d_ptr->_path = trashPath;
101
if (!d_ptr->_path.startsWith(trashPath))
103
d_ptr->_isValid = false;
106
QString abs(d_ptr->_path);
107
d_ptr->_normalizedPath = abs.replace(0,trashPath.length()+1, LocationUrl::TrashRootURL);
111
TrashItemInfo& TrashItemInfo::operator=(const DirItemInfo &other)
113
DirItemInfo::operator = (other);
114
//the following code is disabled because TrashItemInfo does not define any data member
115
//but it is kept for the case any specific data member be necessary in the future
116
//and it is also kept to warn that doing so would cause a bug
118
const TrashItemInfo *isTrash = dynamic_cast<const TrashItemInfo*> (&other);
121
//copy data specific to this class here,
122
//do not unnecessarily copy data that is handled by parent's assignment operator
129
TrashItemInfo& TrashItemInfo::operator=(const TrashItemInfo &other)
131
DirItemInfo::operator = (other);
137
* \brief TrashItemInfo::getTrashDir()
139
* Lets suppose a directory in the trash named DIR:
140
* absFilePath() = /home/user/.local/share/Trash/files/DIR
141
* normalizedFilePath() = trash:///DIR
143
* The trash dir is /home/user/.local/share/Trash/files
145
* \return The trash dir
147
QString TrashItemInfo::getTrashDir() const
150
QString norm(urlPath());
151
if ( norm.length() > LocationUrl::TrashRootURL.length()
152
&& norm.startsWith(LocationUrl::TrashRootURL)
155
QStringRef trashItemRef(norm.midRef(LocationUrl::TrashRootURL.length()));
156
QString abs(absoluteFilePath());
157
int length = abs.lastIndexOf(trashItemRef);
160
trashDir = abs.left(length-1);
167
QString TrashItemInfo::getRootTrashDir() const
169
QString ret = getTrashDir();
172
ret = QFileInfo(ret).absolutePath();