diff options
Diffstat (limited to 'src/cli_cfiles.cc')
-rw-r--r-- | src/cli_cfiles.cc | 170 |
1 files changed, 170 insertions, 0 deletions
diff --git a/src/cli_cfiles.cc b/src/cli_cfiles.cc new file mode 100644 index 0000000..b32083c --- /dev/null +++ b/src/cli_cfiles.cc | |||
@@ -0,0 +1,170 @@ | |||
1 | // cli_cfiles.cc for Fluxbox Window Manager | ||
2 | // Copyright (c) 2014 - Mathias Gumz <akira at fluxbox.org> | ||
3 | // | ||
4 | // Permission is hereby granted, free of charge, to any person obtaining a | ||
5 | // copy of this software and associated documentation files (the "Software"), | ||
6 | // to deal in the Software without restriction, including without limitation | ||
7 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
8 | // and/or sell copies of the Software, and to permit persons to whom the | ||
9 | // Software is furnished to do so, subject to the following conditions: | ||
10 | // | ||
11 | // The above copyright notice and this permission notice shall be included in | ||
12 | // all copies or substantial portions of the Software. | ||
13 | // | ||
14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
17 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
19 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
20 | // DEALINGS IN THE SOFTWARE. | ||
21 | |||
22 | #include "cli.hh" | ||
23 | #include "defaults.hh" | ||
24 | |||
25 | #include "Debug.hh" | ||
26 | #include "FbTk/FileUtil.hh" | ||
27 | #include "FbTk/I18n.hh" | ||
28 | #include "FbTk/Resource.hh" | ||
29 | #include "FbTk/StringUtil.hh" | ||
30 | |||
31 | #ifdef HAVE_CONFIG_H | ||
32 | #include "config.h" | ||
33 | #endif // HAVE_CONFIG_H | ||
34 | |||
35 | #ifdef HAVE_CSTRING | ||
36 | #include <cstring> | ||
37 | #else | ||
38 | #include <string.h> | ||
39 | #endif | ||
40 | |||
41 | #ifdef HAVE_SYS_STAT_H | ||
42 | #include <sys/types.h> | ||
43 | #include <sys/stat.h> | ||
44 | #endif // HAVE_SYS_STAT_H | ||
45 | |||
46 | #ifdef HAVE_UNISTD_H | ||
47 | #include <unistd.h> | ||
48 | #endif | ||
49 | |||
50 | #ifdef HAVE_CSTDLIB | ||
51 | #include <cstdlib> | ||
52 | #else | ||
53 | #include <stdlib.h> | ||
54 | #endif | ||
55 | |||
56 | |||
57 | using std::string; | ||
58 | using std::endl; | ||
59 | using std::cerr; | ||
60 | |||
61 | |||
62 | #ifdef _WIN32 | ||
63 | /** | ||
64 | Wrapper function for Windows builds - mkdir takes only one param. | ||
65 | */ | ||
66 | static int mkdir(const char *dirname, int /*permissions*/) { | ||
67 | return mkdir(dirname); | ||
68 | } | ||
69 | #endif | ||
70 | |||
71 | |||
72 | /** | ||
73 | setup the configutation files in | ||
74 | home directory | ||
75 | */ | ||
76 | void FluxboxCli::setupConfigFiles(const std::string& dirname, const std::string& rc) { | ||
77 | |||
78 | _FB_USES_NLS; | ||
79 | |||
80 | const bool has_dir = FbTk::FileUtil::isDirectory(dirname.c_str()); | ||
81 | |||
82 | |||
83 | struct CFInfo { | ||
84 | bool create_file; | ||
85 | const char* default_name; | ||
86 | const std::string filename; | ||
87 | } cfiles[] = { | ||
88 | { !has_dir, DEFAULT_INITFILE, rc }, | ||
89 | { !has_dir, DEFAULTKEYSFILE, dirname + "/keys" }, | ||
90 | { !has_dir, DEFAULTMENU, dirname + "/menu" }, | ||
91 | { !has_dir, DEFAULT_APPSFILE, dirname + "/apps" }, | ||
92 | { !has_dir, DEFAULT_OVERLAY, dirname + "/overlay" }, | ||
93 | { !has_dir, DEFAULT_WINDOWMENU, dirname + "/windowmenu" } | ||
94 | }; | ||
95 | const size_t nr_of_cfiles = sizeof(cfiles)/sizeof(CFInfo); | ||
96 | |||
97 | |||
98 | if (has_dir) { // check if anything with these names exists, if not create new | ||
99 | for (size_t i = 0; i < nr_of_cfiles; ++i) { | ||
100 | cfiles[i].create_file = access(cfiles[i].filename.c_str(), F_OK); | ||
101 | } | ||
102 | } else { | ||
103 | |||
104 | fbdbg << "Creating dir: " << dirname << endl; | ||
105 | if (mkdir(dirname.c_str(), 0700)) { | ||
106 | fprintf(stderr, _FB_CONSOLETEXT(Fluxbox, ErrorCreatingDirectory, | ||
107 | "Can't create %s directory", | ||
108 | "Can't create a directory, one %s for directory name").c_str(), | ||
109 | dirname.c_str()); | ||
110 | cerr << endl; | ||
111 | return; | ||
112 | } | ||
113 | } | ||
114 | |||
115 | bool sync_fs = false; | ||
116 | |||
117 | // copy default files if needed | ||
118 | for (size_t i = 0; i < nr_of_cfiles; ++i) { | ||
119 | if (cfiles[i].create_file) { | ||
120 | FbTk::FileUtil::copyFile(FbTk::StringUtil::expandFilename(cfiles[i].default_name).c_str(), cfiles[i].filename.c_str()); | ||
121 | sync_fs = true; | ||
122 | } | ||
123 | } | ||
124 | #ifdef HAVE_SYNC | ||
125 | if (sync_fs) { | ||
126 | sync(); | ||
127 | } | ||
128 | #endif | ||
129 | } | ||
130 | |||
131 | |||
132 | |||
133 | // configs might be out of date, so run fluxbox-update_configs | ||
134 | // if necassary. | ||
135 | void FluxboxCli::updateConfigFilesIfNeeded(const std::string& rc_file) { | ||
136 | |||
137 | FbTk::ResourceManager r_mgr(rc_file.c_str(), false); | ||
138 | FbTk::Resource<int> c_version(r_mgr, 0, "session.configVersion", "Session.ConfigVersion"); | ||
139 | |||
140 | if (!r_mgr.load(rc_file.c_str())) { | ||
141 | _FB_USES_NLS; | ||
142 | cerr << _FB_CONSOLETEXT(Fluxbox, CantLoadRCFile, "Failed to load database", "") | ||
143 | << ": " | ||
144 | << rc_file << endl; | ||
145 | return; | ||
146 | } | ||
147 | |||
148 | if (*c_version < CONFIG_VERSION) { | ||
149 | |||
150 | fbdbg << "updating config files from version " | ||
151 | << *c_version | ||
152 | << " to " | ||
153 | << CONFIG_VERSION | ||
154 | << endl; | ||
155 | |||
156 | string commandargs = realProgramName("fluxbox-update_configs"); | ||
157 | commandargs += " -rc " + rc_file; | ||
158 | |||
159 | if (system(commandargs.c_str())) { | ||
160 | fbdbg << "running '" | ||
161 | << commandargs | ||
162 | << "' failed." << endl; | ||
163 | } | ||
164 | #ifdef HAVE_SYNC | ||
165 | sync(); | ||
166 | #endif // HAVE_SYNC | ||
167 | } | ||
168 | } | ||
169 | |||
170 | |||