aboutsummaryrefslogtreecommitdiff
path: root/src-qt5/core/lumina-theme-engine/src/lthemeengine/lthemeengine.cpp
blob: 9ffe39f8474e0bf78e2626501d2e85535fa588da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include <QDir>
#include <QLocale>
#include "lthemeengine.h"

#ifndef LTHEMEENGINE_DATADIR
#define LTHEMEENGINE_DATADIR "/usr/local/share"
#endif

#include <QDebug>

QStringList lthemeengine::readFile(QString filepath){
  QStringList out;
  QFile file(filepath);
  if(file.open(QIODevice::Text | QIODevice::ReadOnly)){
    QTextStream in(&file);
    while(!in.atEnd()){
      out << in.readLine();
    }
    file.close();
  }
  return out;
}

QString lthemeengine::configPath(){
  return QDir::homePath() + "/.config/lthemeengine/";
}

QString lthemeengine::configFile(){
  return configPath() + "lthemeengine.conf";
}

QStringList lthemeengine::iconPaths(){
  QString xdgDataDirs = qgetenv("XDG_DATA_DIRS");
  QString xdgDataHome = qgetenv("XDG_DATA_HOME");
  QStringList paths;
  paths << QDir::homePath() + "/.icons/";
  if(xdgDataDirs.isEmpty()){
    paths << "/usr/share/icons";
    paths << "/usr/local/share/icons";
    }
  else{
    foreach (QString p, xdgDataDirs.split(":")){ paths << QDir(p + "/icons/").absolutePath(); }
    }
  if(xdgDataHome.isEmpty()){ xdgDataHome = QDir::homePath() + "/.local/share"; }
  paths << "/usr/share/pixmaps";
  paths << xdgDataHome + "/icons";
  paths.removeDuplicates();
  //remove invalid
  foreach (QString p, paths){
    if(!QDir(p).exists()){ paths.removeAll(p); }
    }
  return paths;
}

QString lthemeengine::userStyleSheetPath(){
  return configPath() + "qss/";
}

QStringList lthemeengine::sharedStyleSheetPath(){
  QStringList dirs;
  dirs << QString(getenv("XDG_CONFIG_HOME"));
  dirs << QString(getenv("XDG_CONFIG_DIRS")).split(":");
  dirs << QString(getenv("XDG_DATA_DIRS")).split(":");
  for(int i=0; i<dirs.length(); i++){
      if (!dirs[i].endsWith("/")){ dirs[i].append("/"); }
      dirs[i].append("lthemeengine/qss/");
  }
  if(dirs.isEmpty()){ dirs << LTHEMEENGINE_DATADIR"/lthemeengine/qss/"; } //no XDG settings - use the hardcoded path
  return dirs;
 }

QString lthemeengine::userDesktopStyleSheetPath(){
  return configPath() + "desktop_qss/";
}

QStringList lthemeengine::sharedDesktopStyleSheetPath(){
  QStringList dirs;
  dirs << QString(getenv("XDG_CONFIG_HOME"));
  dirs << QString(getenv("XDG_CONFIG_DIRS")).split(":");
  dirs << QString(getenv("XDG_DATA_DIRS")).split(":");
  for(int i=0; i<dirs.length(); i++){
      if (!dirs[i].endsWith("/")){ dirs[i].append("/"); }
      dirs[i].append("lthemeengine/desktop_qss/");
  }
  if(dirs.isEmpty()){ dirs << LTHEMEENGINE_DATADIR"/lthemeengine/desktop_qss/"; } //no XDG settings - use the hardcoded path
  return dirs;
 }

QString lthemeengine::userColorSchemePath(){
  return configPath() + "colors/";
}

QStringList lthemeengine::sharedColorSchemePath(){
  QStringList dirs;
  dirs << QString(getenv("XDG_CONFIG_HOME"));
  dirs << QString(getenv("XDG_CONFIG_DIRS")).split(":");
  dirs << QString(getenv("XDG_DATA_DIRS")).split(":");
  for(int i=0; i<dirs.length(); i++){
      if (!dirs[i].endsWith("/")){ dirs[i].append("/"); }
      dirs[i].append("lthemeengine/colors/");
  }
  if(dirs.isEmpty()){ dirs << LTHEMEENGINE_DATADIR"/lthemeengine/colors/"; } //no XDG settings - use the hardcoded path
  qDebug() << "Got Color Dirs:" << dirs;
  return dirs;
}

QString lthemeengine::systemLanguageID(){
#ifdef Q_OS_UNIX
  QByteArray v = qgetenv ("LC_ALL");
  if (v.isEmpty()){ v = qgetenv ("LC_MESSAGES"); }
  if (v.isEmpty()){ v = qgetenv ("LANG"); }
  if (!v.isEmpty()){ return QLocale (v).name(); }
#endif
  return  QLocale::system().name();
}

QStringList lthemeengine::availableSystemCursors(){	//returns: [name] for each item
  //Find all the directories which could contain themes
 QStringList paths;
  paths << QDir::homePath()+"/.icons";
  QStringList xdd = QString(getenv("XDG_DATA_HOME")).split(":");
  xdd << QString(getenv("XDG_DATA_DIRS")).split(":");
  for(int i=0; i<xdd.length(); i++){
    if(QFile::exists(xdd[i]+"/icons")){
      paths << xdd[i]+"/icons";
    }
  }
  //Now get all the icon themes in these directories
  QStringList themes, tmpthemes;
  QDir dir;
  for(int i=0; i<paths.length(); i++){
    if(dir.cd(paths[i])){
       tmpthemes = dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name);
       for(int j=0; j<tmpthemes.length(); j++){
	 if(tmpthemes[j].startsWith("default")){ continue; }
         if( QFile::exists(dir.absoluteFilePath(tmpthemes[j]+"/cursors")) ){ themes << tmpthemes[j]; }
       }
    }
  }
  //Clean up the list and return
  themes.removeDuplicates();
  themes.sort();
  return themes;
}

//Return the currently-selected Cursor theme
QString lthemeengine::currentCursor(){
  //qDebug() << "Reading Current Cursor Theme:";
  QStringList info = readFile(QDir::homePath()+"/.icons/default/index.theme");
  if(info.isEmpty()){ return ""; }
  QString cursor;
  bool insection = false;
  for(int i=0; i<info.length(); i++){
    if(info[i]=="[Icon Theme]"){ insection = true; continue;}
    else if(insection && info[i].startsWith("Inherits=")){
      cursor = info[i].section("=",1,1).simplified();
      break;
    }
  }
  //qDebug() << " - found theme:" << cursor;
  return cursor;
}

//Change the current Cursor Theme
bool lthemeengine::setCursorTheme(QString cursorname){
//qDebug() << "Set Cursor Theme:" << cursorname;
  if(cursorname=="default"){
    //special case - this will cause a recursive inheritance loop - just remove the file instead
    if(QFile::exists(QDir::homePath()+"/.icons/default/index.theme")){
      return QFile::remove(QDir::homePath()+"/.icons/default/index.theme");
    }
    return true; //already does not exist
  }
  QStringList info = readFile(QDir::homePath()+"/.icons/default/index.theme");
    bool insection = false;
    bool changed = false;
    QString newval = "Inherits="+cursorname;
    for(int i=0; i<info.length() && !changed; i++){
      if(info[i]=="[Icon Theme]"){
	insection = true;
      }else if( info[i].startsWith("[") && insection){
	//Section does not have the setting - add it
	info.insert(i, newval);
	changed =true;
      }else if( info[i].startsWith("[") ){
	insection = false;
      }else if(insection && info[i].startsWith("Inherits=")){
        info[i] = newval; //replace the current setting
        changed = true;
      }
    } //end loop over file contents
    if(!changed){ //Could not change the file contents for some reason
      if(insection){ info << newval; } //end of file while in the section
      else{ info << "[Icon Theme]" << newval; } //entire section missing from file
    }
    //Now save the file
    QFile file(QDir::homePath()+"/.icons/default/index.theme");
    bool ok = false;
    if( file.open(QIODevice::WriteOnly | QIODevice::Truncate) ){
      QTextStream out(&file);
      out << info.join("\n");
      if(!info.last().isEmpty()){ out << "\n"; } //always end with a new line
      file.close();
      ok = true;
    }
    //qDebug() << "Done saving the cursor:" << info;
    return ok;
}
bgstack15