-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileHelper.cpp
110 lines (84 loc) · 2.99 KB
/
fileHelper.cpp
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
#include <glibmm/refptr.h>
#include <glibmm/miscutils.h>
#include <glibmm/fileutils.h>
#include <giomm/init.h>
#include <giomm/file.h>
#include "fileHelper.h"
#include <iostream>
// this is definitely not win32 compatible
namespace fileHelper {
std::string duckTapegetenv(const std::string &variable)
{
// so, at least on Arch Linux with glibmm 2.46.2-1, Glib::getenv(const std::string&) returns 0x0
// which crashes the whole thing, since you can't build std::strings from 0x0.
// so we wrap around the overload that actually works.
// 2015/12/13: Glib::getenv(const std::string&) seems to be working with glibmm 2.46.3 now.
bool found; // hey look I'm useless!
return Glib::getenv(variable, found);
}
void checkForOurDirectories()
{
using namespace std;
using namespace Glib;
using namespace Gio;
Gio::init();
// check for XDG_CONFIG_HOME
string env = duckTapegetenv("XDG_CONFIG_HOME");
if(!env.empty()) {
string path = env + "/livestreamer-gtkmm3/";
if(!file_test(path, FileTest::FILE_TEST_EXISTS)) {
RefPtr<File> f = File::create_for_path(path);
f->make_directory();
}
} else {
// check ~/.config/livestreamer-gtkmm3/
string path = get_home_dir() + "/.config/livestreamer-gtkmm3/";
if(!file_test(path, FileTest::FILE_TEST_EXISTS)) {
RefPtr<File> f = File::create_for_path(path);
f->make_directory();
}
}
}
std::string getConfigFilePath(const std::string &fileName)
{
using namespace std;
using namespace Glib;
// first, look for the file in XDG_CONFIG_HOME/livestreamer-gtkmm3
// if that isn't set, use ~/.config/livestreamer-gtkmm3
string env = duckTapegetenv("XDG_CONFIG_HOME");
if(!env.empty()) {
return env + "/livestreamer-gtkmm3/" + fileName;
}
return get_home_dir() + "/.config/livestreamer-gtkmm3/" + fileName;
}
std::string getDataFilePath(const std::string &fileName)
{
using namespace std;
using namespace Glib;
// if this is a debug build, look for the file in the current directory
// else, look for XDG_DATA_HOME/livestreamer-gtkmm3
// if that isn't set, check for ~/.local/share/livestreamer-gtkmm3
#ifndef NDEBUG
return "./" + fileName;
#endif
string env = duckTapegetenv("XDG_DATA_HOME");
if(!env.empty()) {
string path = env + "/livestreamer-gtkmm3/" + fileName;
if(file_test(path, FileTest::FILE_TEST_EXISTS)) {
return path;
}
} else {
string path = get_home_dir() + "/.local/share/livestreamer-gtkmm3/" + fileName;
if(file_test(path, FileTest::FILE_TEST_EXISTS)) {
return path;
}
}
// if everything fails, try this.
string path = "/usr/share/livestreamer-gtkmm3/" + fileName;
if(file_test(path, FileTest::FILE_TEST_EXISTS)) {
return path;
}
// if that fails, give up
throw std::runtime_error("Could not find any data directory, check your installation.");
}
}