workman161 / glovebox

An in-car assistant built with Qt Embedded Linux.

This URL has Read+Write access

glovebox / gloveui / GStandardDirs.cpp
100644 86 lines (70 sloc) 1.961 kb
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
#include "GStandardDirs.h"
 
#include "config.h"
 
#include <QtCore/QFile>
#include <QtCore/QDir>
 
#include <stdlib.h>
 
#include <QDebug>
 
static const char suffixes[] =
"icons\0"
"icons\0"
"sounds\0"
"sounds\0"
"\0";
 
static const int suffixes_indexes[] = {
  0, 6, 12, 19, -1
};
 
GStandardDirs::GStandardDirs()
{
  uint index = 0;
  while (suffixes_indexes[index] != -1) {
    addResourceType(suffixes + suffixes_indexes[index], suffixes + suffixes_indexes[index+1]);
    index+=2;
  }
  addPrefix(GLOVEBOX_DATA_DIR);
  
  QStringList xdg_paths = QString(QFile::decodeName(getenv("XDG_DATA_DIRS"))).split(':');
  foreach(const QString xdgDir, xdg_paths)
    addPrefix(QDir::fromNativeSeparators(xdgDir));
    
  QStringList glovebox_paths = QString(QFile::decodeName(getenv("GLOVEBOX_DIRS"))).split(':');
  foreach(const QString gloveDir, glovebox_paths)
    addPrefix(QDir::fromNativeSeparators(gloveDir));
  
  addPrefix(".");
 
  addPrefix(QDir::homePath()+"/.glovebox/share");
}
 
void
GStandardDirs::addResourceType(const char * type, const char * relativePath)
{
  qDebug() << "Added" << relativePath << "to" << type << "list";
  types.insert(type, relativePath);
}
 
void
GStandardDirs::addPrefix(const QString & prefix)
{
  qDebug() << "Added prefix" << prefix;
  prefixes.append(prefix);
}
 
QString
GStandardDirs::findFile(const QString &type, const QString &file)
{
  QStringList files = findFiles(type,file);
  if (files.empty())
    return QString();
  return findFiles(type, file).at(0);
}
 
QStringList
GStandardDirs::findFiles(const QString &type, const QString &file)
{
  if (cache.contains(type+"/"+file))
    return cache[type+"/"+file];
  QStringList ret;
  if (types.contains(type)) {
    QString suffix = types[type];
    foreach(const QString prefix, prefixes) {
      QString path = prefix+"/"+suffix+"/"+file;
      if (QFile::exists(path)) {
        ret.append(path);
      }
    }
    cache.insert(type+"/"+file, ret);
  }
  return ret;
}