Browse Source

Even better handling of home directory

merge-requests/365/head
Jørgen Lien Sellæg 10 years ago
parent
commit
09f4f517c1
  1. 20
      src/filesystem.cc
  2. 1
      src/filesystem.h
  3. 23
      src/singletons.cc
  4. 11
      src/singletons.h

20
src/filesystem.cc

@ -22,6 +22,9 @@ std::string safe_get_env(const std::string &env) {
return nullptr==ptr ? "" : std::string(ptr);
}
/**
* Returns home folder, empty on error
*/
std::string juci::filesystem::get_home_folder() {
auto home=safe_get_env("HOME");
if(home.empty())
@ -30,11 +33,24 @@ std::string juci::filesystem::get_home_folder() {
if((status.permissions() & 0222)>=2) {
return home;
} else {
Singleton::terminal()->print("Invalid permissions. Cannot write in " + home + "\n");
throw new std::exception;
JERROR("No write permissions in home, var:");
DEBUG_VAR(home);
return "";
}
}
/**
* Returns tmp folder, empty on error.
*/
std::string juci::filesystem::get_tmp_folder() {
boost::system::error_code code;
auto path = boost::filesystem::temp_directory_path(code);
if (code.value()!=0) {
return "";
}
return path.string();
}
int juci::filesystem::read(const std::string &path, Glib::RefPtr<Gtk::TextBuffer> text_buffer) {
std::ifstream input(path, std::ofstream::binary);

1
src/filesystem.h

@ -26,6 +26,7 @@ namespace juci {
static bool write(const std::string &path, Glib::RefPtr<Gtk::TextBuffer> text_buffer);
static bool write(const boost::filesystem::path &path, Glib::RefPtr<Gtk::TextBuffer> text_buffer) { return write(path.string(), text_buffer); }
static std::string get_home_folder();
static std::string get_tmp_folder();
};
} // namepace juci
#endif // JUCI_SOURCEFILE_H_

23
src/singletons.cc

@ -30,3 +30,26 @@ Gtk::Label *Singleton::info() {
info_=std::unique_ptr<Gtk::Label>(new Gtk::Label());
return info_.get();
}
std::string Singleton::create_config_path(const std::string &subfolder) {
boost::filesystem::path home;
home = juci::filesystem::get_home_folder();
if(home.empty()) {
Singleton::terminal()->print("Could not find/write to home directory. Using defaults, no settings will be saved.");
home = juci::filesystem::get_tmp_folder();
if(home.empty()) {
std::string message("Please fix permissions of your home folder");
std::cerr << message << std::endl;
JFATAL(message);
throw new std::exception;
}
}
home /= subfolder;
return home.string();
}
std::string Singleton::config_dir() { return create_config_path(".juci/config"); }
std::string Singleton::log_dir() { return create_config_path(".juci/log"); }
std::string Singleton::style_dir() { return create_config_path(".juci/styles"); }

11
src/singletons.h

@ -11,6 +11,7 @@
#include <gtkmm.h>
#include <string>
#include "filesystem.h"
#include <iostream>
class Singleton {
public:
@ -27,12 +28,10 @@ public:
static std::unique_ptr<Directories::Config> directories_;
static std::unique_ptr<Terminal::Config> terminal_;
};
static std::string create_config_path(const std::string &subfolder) {
return juci::filesystem::get_home_folder() + subfolder;
}
static std::string config_dir() { return create_config_path("/.juci/config/"); }
static std::string log_dir() { return create_config_path("/.juci/log/"); }
static std::string style_dir() { return create_config_path("/.juci/styles/"); }
static std::string create_config_path(const std::string &subfolder);
static std::string config_dir();
static std::string log_dir();
static std::string style_dir();
static Terminal *terminal();
static Directories *directories();
static Gtk::Label *status();

Loading…
Cancel
Save