aboutsummaryrefslogtreecommitdiff
path: root/src/cli_cfiles.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/cli_cfiles.cc')
-rw-r--r--src/cli_cfiles.cc166
1 files changed, 166 insertions, 0 deletions
diff --git a/src/cli_cfiles.cc b/src/cli_cfiles.cc
new file mode 100644
index 0000000..5da1ba2
--- /dev/null
+++ b/src/cli_cfiles.cc
@@ -0,0 +1,166 @@
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_CSTRING
32 #include <cstring>
33#else
34 #include <string.h>
35#endif
36
37#ifdef HAVE_SYS_STAT_H
38#include <sys/types.h>
39#include <sys/stat.h>
40#endif // HAVE_SYS_STAT_H
41
42#ifdef HAVE_UNISTD_H
43 #include <unistd.h>
44#endif
45
46#ifdef HAVE_CSTDLIB
47 #include <cstdlib>
48#else
49 #include <stdlib.h>
50#endif
51
52
53using std::string;
54using std::endl;
55using std::cerr;
56
57
58#ifdef _WIN32
59/**
60 Wrapper function for Windows builds - mkdir takes only one param.
61*/
62static int mkdir(const char *dirname, int /*permissions*/) {
63 return mkdir(dirname);
64}
65#endif
66
67
68/**
69 setup the configutation files in
70 home directory
71*/
72void FluxboxCli::setupConfigFiles(const std::string& dirname, const std::string& rc) {
73
74 _FB_USES_NLS;
75
76 const bool has_dir = FbTk::FileUtil::isDirectory(dirname.c_str());
77
78
79 struct CFInfo {
80 bool create_file;
81 const char* default_name;
82 const std::string filename;
83 } cfiles[] = {
84 { !has_dir, DEFAULT_INITFILE, rc },
85 { !has_dir, DEFAULTKEYSFILE, dirname + "/keys" },
86 { !has_dir, DEFAULTMENU, dirname + "/menu" },
87 { !has_dir, DEFAULT_APPSFILE, dirname + "/apps" },
88 { !has_dir, DEFAULT_OVERLAY, dirname + "/overlay" },
89 { !has_dir, DEFAULT_WINDOWMENU, dirname + "/windowmenu" }
90 };
91 const size_t nr_of_cfiles = sizeof(cfiles)/sizeof(CFInfo);
92
93
94 if (has_dir) { // check if anything with these names exists, if not create new
95 for (size_t i = 0; i < nr_of_cfiles; ++i) {
96 cfiles[i].create_file = access(cfiles[i].filename.c_str(), F_OK);
97 }
98 } else {
99
100 fbdbg << "Creating dir: " << dirname << endl;
101 if (mkdir(dirname.c_str(), 0700)) {
102 fprintf(stderr, _FB_CONSOLETEXT(Fluxbox, ErrorCreatingDirectory,
103 "Can't create %s directory",
104 "Can't create a directory, one %s for directory name").c_str(),
105 dirname.c_str());
106 cerr << endl;
107 return;
108 }
109 }
110
111 bool sync_fs = false;
112
113 // copy default files if needed
114 for (size_t i = 0; i < nr_of_cfiles; ++i) {
115 if (cfiles[i].create_file) {
116 FbTk::FileUtil::copyFile(FbTk::StringUtil::expandFilename(cfiles[i].default_name).c_str(), cfiles[i].filename.c_str());
117 sync_fs = true;
118 }
119 }
120#ifdef HAVE_SYNC
121 if (sync_fs) {
122 sync();
123 }
124#endif
125}
126
127
128
129// configs might be out of date, so run fluxbox-update_configs
130// if necassary.
131void FluxboxCli::updateConfigFilesIfNeeded(const std::string& rc_file) {
132
133 FbTk::ResourceManager r_mgr(rc_file.c_str(), false);
134 FbTk::Resource<int> c_version(r_mgr, 0, "session.configVersion", "Session.ConfigVersion");
135
136 if (!r_mgr.load(rc_file.c_str())) {
137 _FB_USES_NLS;
138 cerr << _FB_CONSOLETEXT(Fluxbox, CantLoadRCFile, "Failed to load database", "")
139 << ": "
140 << rc_file << endl;
141 return;
142 }
143
144 if (*c_version < CONFIG_VERSION) {
145
146 fbdbg << "updating config files from version "
147 << *c_version
148 << " to "
149 << CONFIG_VERSION
150 << endl;
151
152 string commandargs = realProgramName("fluxbox-update_configs");
153 commandargs += " -rc " + rc_file;
154
155 if (system(commandargs.c_str())) {
156 fbdbg << "running '"
157 << commandargs
158 << "' failed." << endl;
159 }
160#ifdef HAVE_SYNC
161 sync();
162#endif // HAVE_SYNC
163 }
164}
165
166