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

« back to all changes in this revision

Viewing changes to src/plugin/folderlistmodel/fmutil.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: fmutil.cpp
 
19
 * Date: 29/01/2014
 
20
 */
 
21
 
 
22
#include "fmutil.h"
 
23
 
 
24
#include <QIcon>
 
25
#include <QFileInfo>
 
26
#include <QDir>
 
27
#include <QDebug>
 
28
 
 
29
bool FMUtil::m_triedThemeName = false;
 
30
 
 
31
FMUtil::FMUtil()
 
32
{
 
33
}
 
34
 
 
35
 
 
36
/*!
 
37
 * \brief FMUtil::setThemeName() tries to set a theme name in order to get icons
 
38
 */
 
39
void FMUtil::setThemeName()
 
40
{
 
41
    QString name;
 
42
    //set saying we have tried to set ThemeName
 
43
    m_triedThemeName = true;
 
44
    QLatin1String  ubuntu_mobileTheme("ubuntu-mobile");
 
45
    QStringList paths(QIcon::themeSearchPaths());
 
46
#if defined(Q_OS_UNIX)
 
47
    if (paths.isEmpty())
 
48
    {
 
49
        paths.append(QLatin1String("/usr/share/icons"));
 
50
    }
 
51
#endif
 
52
    foreach (const QString&  dir, paths)
 
53
    {
 
54
        QDir D(dir);
 
55
        if (D.exists())
 
56
        {
 
57
#if DEBUG_MESSAGES
 
58
      qDebug() << Q_FUNC_INFO << "trying theme on Dir" << D.path();
 
59
#endif
 
60
           QFileInfoList inf =  D.entryInfoList(QDir::AllDirs | QDir::NoDotAndDotDot | QDir::System );
 
61
           int counter = inf.count();
 
62
           //specific names
 
63
           while (counter--)
 
64
           {
 
65
               if (inf.at(counter).fileName() == ubuntu_mobileTheme)
 
66
               {
 
67
                   if (testThemeName(ubuntu_mobileTheme))
 
68
                   {
 
69
                       return;
 
70
                   }
 
71
                   else
 
72
                   {
 
73
                       inf.removeAt(counter);
 
74
                   }
 
75
               }
 
76
           }
 
77
           //try symlinks
 
78
           counter = inf.count();
 
79
           while (counter--)
 
80
           {
 
81
               if (inf.at(counter).isSymLink())
 
82
               {
 
83
                   if (testThemeName(inf.at(counter).fileName()))
 
84
                   {
 
85
                       return;
 
86
                   }
 
87
                   else
 
88
                   {
 
89
                       inf.removeAt(counter);
 
90
                   }
 
91
               }
 
92
           }
 
93
           //try common directories
 
94
           counter = inf.count();
 
95
           while (counter--)
 
96
           {
 
97
               if (testThemeName(inf.at(counter).fileName()))
 
98
               {
 
99
                   return;
 
100
               }
 
101
           }
 
102
        }
 
103
    }
 
104
    name.clear();
 
105
    QIcon::setThemeName(name);
 
106
}
 
107
 
 
108
 
 
109
bool FMUtil::testThemeName(const QString& themeName)
 
110
{
 
111
    QMimeDatabase mimeBase;
 
112
    QStringList mimesToTest = QStringList()
 
113
                             << "text/plain"
 
114
                             << "inode/directory"
 
115
                             << "application/pdf"
 
116
                             << "application/postscript"
 
117
                             << "application/x-gzip";
 
118
 
 
119
    QIcon::setThemeName(themeName);
 
120
    bool hasTheme = true;
 
121
    int counter = mimesToTest.count();
 
122
    while(hasTheme  && counter--)
 
123
    {
 
124
        QMimeType mimetype = mimeBase.mimeTypeForName(mimesToTest.at(counter));
 
125
        hasTheme = QIcon::hasThemeIcon( mimetype.iconName() ) ||
 
126
                   QIcon::hasThemeIcon( mimetype.genericIconName() ) ;
 
127
    }
 
128
#if DEBUG_MESSAGES
 
129
    qDebug() << Q_FUNC_INFO << "trying theme name" << themeName << "ret=" << hasTheme;
 
130
#endif
 
131
    return hasTheme;
 
132
}