Skip to content

Commit

Permalink
FLUID: deglobalisation
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthiasWM committed Nov 13, 2024
1 parent 022d4ee commit df89a06
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 162 deletions.
120 changes: 118 additions & 2 deletions fluid/application/application.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@
#include "fluid.h"
#include "widget_browser.h"

extern bool confirm_project_clear();
#include <FL/fl_ask.H>
#include "../src/flstring.h"

#include <unistd.h>

using namespace fluid;

/**
Clear the current project and create a new, empty one.
Expand All @@ -34,7 +39,7 @@ extern bool confirm_project_clear();
*/
bool fluid::Application::new_project(bool user_must_confirm) {
// verify user intention
if ((user_must_confirm) && (confirm_project_clear() == false))
if ((user_must_confirm) && (project().confirm_clear() == false))
return false;

// clear the current project
Expand All @@ -56,3 +61,114 @@ fluid::Project &fluid::Application::project()
return g_project;
}


/**
Generate a path to a directory for temporary data storage.
The path is stored in g_tmpdir.
*/
void Application::create_tmpdir() {
if (tmpdir_create_called)
return;
tmpdir_create_called = true;

char buf[128];
#if _WIN32
// The usual temp file locations on Windows are
// %system%\Windows\Temp
// %userprofiles%\AppData\Local
// usually resolving into
// C:/Windows/Temp/
// C:\Users\<username>\AppData\Local\Temp
fl_snprintf(buf, sizeof(buf)-1, "fluid-%d/", (long)GetCurrentProcessId());
Fl_String name = buf;
wchar_t tempdirW[FL_PATH_MAX+1];
char tempdir[FL_PATH_MAX+1];
unsigned len = GetTempPathW(FL_PATH_MAX, tempdirW);
if (len == 0) {
strcpy(tempdir, "c:/windows/temp/");
} else {
unsigned wn = fl_utf8fromwc(tempdir, FL_PATH_MAX, tempdirW, len);
tempdir[wn] = 0;
}
Fl_String path = tempdir;
end_with_slash(path);
path += name;
fl_make_path(path.c_str());
if (fl_access(path.c_str(), 6) == 0) tmpdir_path = path;
#else
fl_snprintf(buf, sizeof(buf)-1, "fluid-%d/", getpid());
Fl_String name = buf;
Fl_String path = fl_getenv("TMPDIR");
if (!path.empty()) {
end_with_slash(path);
path += name;
fl_make_path(path.c_str());
if (fl_access(path.c_str(), 6) == 0) tmpdir_path = path;
}
if (tmpdir_path.empty()) {
path = Fl_String("/tmp/") + name;
fl_make_path(path.c_str());
if (fl_access(path.c_str(), 6) == 0) tmpdir_path = path;
}
#endif
if (tmpdir_path.empty()) {
char pbuf[FL_PATH_MAX+1];
fluid_prefs.get_userdata_path(pbuf, FL_PATH_MAX);
path = Fl_String(pbuf);
end_with_slash(path);
path += name;
fl_make_path(path.c_str());
if (fl_access(path.c_str(), 6) == 0) tmpdir_path = path;
}
if (tmpdir_path.empty()) {
if (Fluid.batch_mode) {
fprintf(stderr, "ERROR: Can't create directory for temporary data storage.\n");
} else {
fl_alert("Can't create directory for temporary data storage.");
}
}
}

/**
Delete the temporary directory that was created in set_tmpdir.
*/
void Application::delete_tmpdir() {
// was a temporary directory created
if (!tmpdir_create_called)
return;
if (tmpdir_path.empty())
return;

// first delete all files that may still be left in the temp directory
struct dirent **de;
int n_de = fl_filename_list(tmpdir_path.c_str(), &de);
if (n_de >= 0) {
for (int i=0; i<n_de; i++) {
Fl_String path = tmpdir_path + de[i]->d_name;
fl_unlink(path.c_str());
}
fl_filename_free_list(&de, n_de);
}

// then delete the directory itself
if (fl_rmdir(tmpdir_path.c_str()) < 0) {
if (Fluid.batch_mode) {
fprintf(stderr, "WARNING: Can't delete tmpdir '%s': %s", tmpdir_path.c_str(), strerror(errno));
} else {
fl_alert("WARNING: Can't delete tmpdir '%s': %s", tmpdir_path.c_str(), strerror(errno));
}
}
}

/**
Return the path to a temporary directory for this instance of FLUID.
Fluid will do its best to clear and delete this directory when exiting.
\return the path to the temporary directory, ending in a '/', or and empty
string if no directory could be created.
*/
const Fl_String &Application::get_tmpdir() {
if (!tmpdir_create_called)
create_tmpdir();
return tmpdir_path;
}

12 changes: 12 additions & 0 deletions fluid/application/application.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
namespace fluid {

class Application {
/// path to store temporary files during app run
Fl_String tmpdir_path { };
/// true if the temporary file path was already created
bool tmpdir_create_called { false };
public:
/// Command line arguments.
application::Args args { *this };
Expand All @@ -43,6 +47,14 @@ class Application {
bool new_project(bool user_must_confirm = true);
/// Get the current project.
Project &project();

/// Generate a path to a directory for temporary data storage.
void create_tmpdir();
/// Delete the temporary directory that was created in set_tmpdir.
void delete_tmpdir();
/// Return the path to a temporary directory for this instance of FLUID.
const Fl_String &get_tmpdir();

};

}; // namespace FLUID
Expand Down
9 changes: 5 additions & 4 deletions fluid/codeview_panel.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "codeview_panel.h"
#include "fluid.h"
#include "application/application.h"
#include "project/project.h"
#include "file.h"
#include "../src/flstring.h"
Expand Down Expand Up @@ -153,17 +154,17 @@ void update_codeview_cb(class Fl_Button*, void*) {

if (!cv_source_filename) {
cv_source_filename = (char*)malloc(FL_PATH_MAX);
fl_strlcpy(cv_source_filename, get_tmpdir().c_str(), FL_PATH_MAX);
fl_strlcpy(cv_source_filename, Fluid.get_tmpdir().c_str(), FL_PATH_MAX);
fl_strlcat(cv_source_filename, "codeview_tmp.cxx", FL_PATH_MAX);
}
if (!cv_header_filename) {
cv_header_filename = (char*)malloc(FL_PATH_MAX);
fl_strlcpy(cv_header_filename, get_tmpdir().c_str(), FL_PATH_MAX);
fl_strlcpy(cv_header_filename, Fluid.get_tmpdir().c_str(), FL_PATH_MAX);
fl_strlcat(cv_header_filename, "codeview_tmp.h", FL_PATH_MAX);
}
if (!cv_design_filename) {
cv_design_filename = (char*)malloc(FL_PATH_MAX);
fl_strlcpy(cv_design_filename, get_tmpdir().c_str(), FL_PATH_MAX);
fl_strlcpy(cv_design_filename, Fluid.get_tmpdir().c_str(), FL_PATH_MAX);
fl_strlcat(cv_design_filename, "codeview_tmp.fl", FL_PATH_MAX);
}

Expand All @@ -175,7 +176,7 @@ void update_codeview_cb(class Fl_Button*, void*) {
} else if (cv_strings->visible_r()) {
static const char *exts[] = { ".txt", ".po", ".msg" };
char fn[FL_PATH_MAX+1];
fl_strlcpy(fn, get_tmpdir().c_str(), FL_PATH_MAX);
fl_strlcpy(fn, Fluid.get_tmpdir().c_str(), FL_PATH_MAX);
fl_strlcat(fn, "strings", FL_PATH_MAX);
fl_filename_setext(fn, FL_PATH_MAX, exts[static_cast<size_t>(g_project.i18n_type)]);
write_strings(fn);
Expand Down
11 changes: 7 additions & 4 deletions fluid/codeview_panel.fl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ comment {//
decl {\#include "fluid.h"} {private local
}

decl {\#include "application/application.h"} {private local
}

decl {\#include "project/project.h"} {private local
}

Expand Down Expand Up @@ -176,17 +179,17 @@ and load those into the Code Viewer widgets.} open return_type void

if (!cv_source_filename) {
cv_source_filename = (char*)malloc(FL_PATH_MAX);
fl_strlcpy(cv_source_filename, get_tmpdir().c_str(), FL_PATH_MAX);
fl_strlcpy(cv_source_filename, Fluid.get_tmpdir().c_str(), FL_PATH_MAX);
fl_strlcat(cv_source_filename, "codeview_tmp.cxx", FL_PATH_MAX);
}
if (!cv_header_filename) {
cv_header_filename = (char*)malloc(FL_PATH_MAX);
fl_strlcpy(cv_header_filename, get_tmpdir().c_str(), FL_PATH_MAX);
fl_strlcpy(cv_header_filename, Fluid.get_tmpdir().c_str(), FL_PATH_MAX);
fl_strlcat(cv_header_filename, "codeview_tmp.h", FL_PATH_MAX);
}
if (!cv_design_filename) {
cv_design_filename = (char*)malloc(FL_PATH_MAX);
fl_strlcpy(cv_design_filename, get_tmpdir().c_str(), FL_PATH_MAX);
fl_strlcpy(cv_design_filename, Fluid.get_tmpdir().c_str(), FL_PATH_MAX);
fl_strlcat(cv_design_filename, "codeview_tmp.fl", FL_PATH_MAX);
}

Expand All @@ -198,7 +201,7 @@ and load those into the Code Viewer widgets.} open return_type void
} else if (cv_strings->visible_r()) {
static const char *exts[] = { ".txt", ".po", ".msg" };
char fn[FL_PATH_MAX+1];
fl_strlcpy(fn, get_tmpdir().c_str(), FL_PATH_MAX);
fl_strlcpy(fn, Fluid.get_tmpdir().c_str(), FL_PATH_MAX);
fl_strlcat(fn, "strings", FL_PATH_MAX);
fl_filename_setext(fn, FL_PATH_MAX, exts[g_project.i18n_type]);
write_strings(fn);
Expand Down
Loading

0 comments on commit df89a06

Please sign in to comment.