~ubuntu-filemanager-dev/ubuntu-filemanager-app/trunk

« back to all changes in this revision

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

  • Committer: Bileto Bot
  • Date: 2017-04-04 17:06:41 UTC
  • mfrom: (588.1.19 fix-desktop-file)
  • Revision ID: ci-train-bot@canonical.com-20170404170641-1p15lmx8wodlx2ut
* Rename binary file to ubuntu-filemanager-app
* Join plugin packages into the main package 

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: trashiteminfo.cpp
19
 
 * Date: 05/03/2014
20
 
 */
21
 
 
22
 
#include "trashiteminfo.h"
23
 
#include "qtrashutilinfo.h"
24
 
#include "locationurl.h"
25
 
#include <QDebug>
26
 
 
27
 
/*!
28
 
 * \brief TrashItemInfo::TrashItemInfo() This constructor does not receive the Trash path
29
 
 *
30
 
 * \param urlPath  the  full pathname  starting with th \a trashPath as "/home/devubuntu/.local/share/Trash/files/test.txt"
31
 
 */
32
 
TrashItemInfo::TrashItemInfo(const QString &urlPath)
33
 
    : DirItemInfo()  
34
 
{  
35
 
   d_ptr->_path            = urlPath;
36
 
   d_ptr->_isLocal         = true;
37
 
   d_ptr->_normalizedPath  = urlPath;
38
 
   if (urlPath == LocationUrl::TrashRootURL)
39
 
   {
40
 
      setRoot();
41
 
   }
42
 
   else
43
 
   {
44
 
       QTrashUtilInfo trashInfo;
45
 
       trashInfo.setInfoFromTrashItem(urlPath);
46
 
       //try to guess the Trash path
47
 
       if (trashInfo.isValid() && !trashInfo.filesDir.isEmpty())
48
 
       {
49
 
           //Trash path found
50
 
           init(trashInfo.filesDir);
51
 
       }
52
 
       QFileInfo maybeDiskUrl(urlPath);
53
 
       if (maybeDiskUrl.exists())
54
 
       {
55
 
           d_ptr->setFileInfo(maybeDiskUrl);
56
 
       }
57
 
   }
58
 
}
59
 
 
60
 
 
61
 
TrashItemInfo::TrashItemInfo(const TrashItemInfo &other)
62
 
       : DirItemInfo(other)       
63
 
{
64
 
}
65
 
 
66
 
 
67
 
/*!
68
 
 * \brief TrashItemInfo::TrashItemInfo()
69
 
 *
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"
72
 
 */
73
 
TrashItemInfo::TrashItemInfo(const QString& trashPath, const QString &urlPath)
74
 
   : DirItemInfo(urlPath)  
75
 
{
76
 
    init(trashPath);
77
 
}
78
 
 
79
 
 
80
 
void TrashItemInfo::setRoot()
81
 
{
82
 
    d_ptr->_isValid      = true;
83
 
    d_ptr->_isRoot       = true;
84
 
    d_ptr->_isDir        = true;
85
 
    d_ptr->_isReadable   = true;
86
 
    d_ptr->_isExecutable = true;
87
 
    d_ptr->_exists       = true;
88
 
    d_ptr->_fileName.clear();
89
 
}
90
 
 
91
 
 
92
 
void TrashItemInfo::init(const QString& trashPath)
93
 
{
94
 
    if (trashPath == absoluteFilePath())
95
 
    {
96
 
        d_ptr->_path = trashPath;
97
 
        setRoot();
98
 
    }
99
 
    else
100
 
    {
101
 
        if (!d_ptr->_path.startsWith(trashPath))
102
 
        {
103
 
            d_ptr->_isValid = false;
104
 
        }
105
 
    }
106
 
    QString abs(d_ptr->_path);
107
 
    d_ptr->_normalizedPath = abs.replace(0,trashPath.length()+1, LocationUrl::TrashRootURL);
108
 
}
109
 
 
110
 
 
111
 
TrashItemInfo& TrashItemInfo::operator=(const DirItemInfo &other)
112
 
{
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
117
 
#if 0
118
 
    const TrashItemInfo *isTrash = dynamic_cast<const TrashItemInfo*> (&other);   
119
 
    if (isTrash)
120
 
    {
121
 
      //copy data specific to this class here,
122
 
      //do not unnecessarily copy data that is handled by parent's assignment operator
123
 
    }
124
 
#endif
125
 
    return *this;
126
 
}
127
 
 
128
 
 
129
 
TrashItemInfo& TrashItemInfo::operator=(const TrashItemInfo &other)
130
 
{
131
 
    DirItemInfo::operator = (other);   
132
 
    return *this;
133
 
}
134
 
 
135
 
 
136
 
/*!
137
 
 * \brief TrashItemInfo::getTrashDir()
138
 
 *
139
 
 *  Lets suppose a directory in the trash named DIR:
140
 
 *    absFilePath()        = /home/user/.local/share/Trash/files/DIR
141
 
 *    normalizedFilePath() =                            trash:///DIR
142
 
 *
143
 
 *    The trash dir is /home/user/.local/share/Trash/files
144
 
 *
145
 
 * \return The trash dir
146
 
 */
147
 
QString TrashItemInfo::getTrashDir() const
148
 
{
149
 
    QString trashDir;
150
 
    QString norm(urlPath());
151
 
    if (    norm.length() > LocationUrl::TrashRootURL.length()
152
 
         && norm.startsWith(LocationUrl::TrashRootURL)
153
 
       )
154
 
    {
155
 
        QStringRef  trashItemRef(norm.midRef(LocationUrl::TrashRootURL.length()));
156
 
        QString abs(absoluteFilePath());
157
 
        int length = abs.lastIndexOf(trashItemRef);
158
 
        if (length > 0)
159
 
        {
160
 
            trashDir = abs.left(length-1);
161
 
        }
162
 
    }
163
 
    return trashDir;
164
 
}
165
 
 
166
 
 
167
 
QString TrashItemInfo::getRootTrashDir() const
168
 
{
169
 
    QString ret = getTrashDir();
170
 
    if (!isRoot())
171
 
    {
172
 
        ret = QFileInfo(ret).absolutePath();
173
 
    }
174
 
    return ret;
175
 
}