From 256c76213d44c158577707a3ffd394bf0e94e65a Mon Sep 17 00:00:00 2001 From: Jonas Suhr Christensen Date: Tue, 23 Sep 2014 20:23:14 +0200 Subject: Added config parse. --- plugingui/Makefile.am | 1 + plugingui/Makefile.am.plugingui | 1 + plugingui/directory.cc | 40 +++++++--- plugingui/pluginconfig.cc | 165 ++++++++++++++++++++++++++++++++++++++++ plugingui/pluginconfig.h | 48 ++++++++++++ plugingui/plugingui.cc | 28 +++++++ plugingui/plugingui.h | 3 + 7 files changed, 275 insertions(+), 11 deletions(-) create mode 100644 plugingui/pluginconfig.cc create mode 100644 plugingui/pluginconfig.h diff --git a/plugingui/Makefile.am b/plugingui/Makefile.am index 3cf60a5..3d09bb2 100644 --- a/plugingui/Makefile.am +++ b/plugingui/Makefile.am @@ -46,6 +46,7 @@ EXTRA_DIST = \ nativewindow_x11.h \ painter.h \ pixelbuffer.h \ + pluginconfig.h \ plugingui.h \ progressbar.h \ resource.h \ diff --git a/plugingui/Makefile.am.plugingui b/plugingui/Makefile.am.plugingui index 0b4f2d2..f475db6 100644 --- a/plugingui/Makefile.am.plugingui +++ b/plugingui/Makefile.am.plugingui @@ -32,6 +32,7 @@ PLUGIN_GUI_SOURCES = \ $(top_srcdir)/plugingui/knob.cc \ $(top_srcdir)/plugingui/filebrowser.cc \ $(top_srcdir)/plugingui/directory.cc \ + $(top_srcdir)/plugingui/pluginconfig.cc \ $(top_srcdir)/plugingui/image.cc \ $(top_srcdir)/plugingui/combobox.cc \ $(top_srcdir)/plugingui/progressbar.cc \ diff --git a/plugingui/directory.cc b/plugingui/directory.cc index 9ebfe70..1465c86 100644 --- a/plugingui/directory.cc +++ b/plugingui/directory.cc @@ -34,9 +34,7 @@ #include #ifdef WIN32 -#ifdef __MINGW32__ #include -#endif #include #endif @@ -153,7 +151,7 @@ Directory::EntryList Directory::listFiles(std::string path, unsigned char filter if(Directory::isRoot(path) && name == "..") continue; unsigned char entryinfo = 0; - if(isHidden(name)) { + if(isHidden(path + SEP + name)) { entryinfo |= DIRECTORY_HIDDEN; } @@ -183,8 +181,8 @@ Directory::EntryList Directory::listFiles(std::string path, unsigned char filter #ifdef WIN32 - DEBUG(directory, "root is %s\n", Directory::root(path).c_str()); - DEBUG(directory, "current path %s is root? %d", path.c_str(), Directory::isRoot(path)); + DEBUG(directory, "Root is %s\n", Directory::root(path).c_str()); + DEBUG(directory, "Current path %s is root? %d", path.c_str(), Directory::isRoot(path)); if(Directory::isRoot(path)) entries.push_back(".."); #endif @@ -291,15 +289,15 @@ bool Directory::isDir() bool Directory::isDir(std::string path) { - DEBUG(directory, "Is '%s' dir?\n", path.c_str()); + DEBUG(directory, "Is '%s' a directory?\n", path.c_str()); struct stat st; if(stat(path.c_str(), &st) == 0) { if((st.st_mode & S_IFDIR) != 0) { - DEBUG(directory, "Yes\n"); + DEBUG(directory, "\t...yes!\n"); return true; } } - DEBUG(directory, "No\n"); + DEBUG(directory, "\t...no!\n"); return false; } @@ -320,19 +318,39 @@ bool Directory::exists(std::string path) bool Directory::isHidden(std::string path) { - // TODO: Handle hidden and system files in windows + DEBUG(directory, "Is '%s' hidden?\n", path.c_str()); #ifdef WIN32 - return false; -#else + // We dont want to filter out '..' pointing to root of a partition unsigned pos = path.find_last_of("/\\"); std::string entry = path.substr(pos+1); + if(entry == "..") { + return false; + } + DWORD fattribs = GetFileAttributes(path.c_str()); + if(fattribs & FILE_ATTRIBUTE_HIDDEN) { + DEBUG(directory, "\t...yes!\n"); + return true; + } + else if(fattribs & FILE_ATTRIBUTE_SYSTEM) { + DEBUG(directory, "\t...yes!\n"); + return true; + } + else { + DEBUG(directory, "\t...no!\n"); + return false; + } +#else + unsigned pos = path.find_last_of("/\\"); + std::string entry = path.substr(pos+1); if(entry.size() > 1 && entry.at(0) == '.' && entry.at(1) != '.') { + DEBUG(directory, "\t...yes!\n"); return true; } else { + DEBUG(directory, "\t...no!\n"); return false; } #endif diff --git a/plugingui/pluginconfig.cc b/plugingui/pluginconfig.cc new file mode 100644 index 0000000..d57d9df --- /dev/null +++ b/plugingui/pluginconfig.cc @@ -0,0 +1,165 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/*************************************************************************** + * config.cc + * + * Tue Jun 3 13:54:05 CEST 2014 + * Copyright 2014 Jonas Suhr Christensen + * jsc@umbraculum.org + ****************************************************************************/ + +/* + * This file is part of DrumGizmo. + * + * DrumGizmo is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * DrumGizmo is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with DrumGizmo; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ +#include "pluginconfig.h" + +#include +#include +#include +#include + +#include "directory.h" + +#ifdef WIN32 +#include +#include +#include +#include +#else +#endif + +#include + +#define CONFIGFILENAME "plugingui.conf" + +#ifdef WIN32 + #define SEP "\\" + #define CONFIGDIRNAME ".drumgizmo" +#else + #define SEP "/" + #define CONFIGDIRNAME ".drumgizmo" +#endif + +Config::Config() +{ + +} + +Config::~Config() +{ + +} + +static std::string configPath() { + #ifdef WIN32 + std::string configpath; + TCHAR szPath[256]; + if(SUCCEEDED(SHGetFolderPath(NULL, + CSIDL_APPDATA | CSIDL_FLAG_CREATE, + NULL, + 0, + szPath))); { + configpath = szPath; + } +#else + std::string configpath = strdup(getenv("HOME")); +#endif + configpath += SEP; + configpath += CONFIGDIRNAME; + + return configpath; +} + +static bool createConfigPath() { + std::string configpath = configPath(); + + if(!Directory::exists(configpath)) { + DEBUG(pluginconfig, "No configuration exists, creating directory '%s'\n", configpath.c_str()); +#ifdef WIN32 + if( (mkdir(configpath.c_str())) < 0) { +#else + if( (mkdir(configpath.c_str(), 0755)) < 0) { +#endif + DEBUG(pluginconfig, "Could not create config directory\n"); + } + return false; + } + + return true; +} + +static FILE* openConfigFile(std::string mode) { + + std::string configpath = configPath(); + + FILE *fp; + std::string configfile = configpath; + configfile += SEP; + configfile += CONFIGFILENAME; + + DEBUG(pluginconfig, "Opening config file '%s'\n", configfile.c_str()); + if(! (fp = fopen(configfile.c_str(), mode.c_str())) ) { + return NULL; + } + + return fp; +} + +void Config::load() +{ + DEBUG(pluginconfig, "Loading config file...\n"); + FILE *fp = openConfigFile("r"); + if(!fp) return; + + lastkit.clear(); + lastmidimap.clear(); + + char buf[4096]; + while( fgets(buf, 4096, fp) ) { + if(!strncmp(buf, "lastkit:", 8)) { + DEBUG(pluginconfig, "Loading last kit path\n"); + // Dont copy newline + if(strlen(buf) > 8 + 1) { + lastkit.append(buf+8, strlen(buf+8) - 1); + DEBUG(pluginconfig, "\t path is %s\n", lastkit.c_str()); + } + } + if(!strncmp(buf, "lastmidimap:", 12)) { + DEBUG(pluginconfig, "Loading lastmidimap path\n"); + // Dont copy newline + if(strlen(buf) > 12+1) lastmidimap.append(buf+12, strlen(buf+12) - 1); + DEBUG(pluginconfig, "\t path is %s\n", lastmidimap.c_str()); + } + } +} + +void Config::save() +{ + DEBUG(pluginconfig, "Saving configuration...\n"); + + createConfigPath(); + + FILE *fp = openConfigFile("w"); + if(!fp) return; + + std::string buf; + buf.append("lastkit:" + lastkit + '\n'); + buf.append("lastmidimap:" + lastmidimap + '\n'); + + fputs(buf.c_str(), fp); + + fclose(fp); +} diff --git a/plugingui/pluginconfig.h b/plugingui/pluginconfig.h new file mode 100644 index 0000000..d0e9bcd --- /dev/null +++ b/plugingui/pluginconfig.h @@ -0,0 +1,48 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/*************************************************************************** + * config.h + * + * Tue Jun 3 13:51:29 CEST 2014 + * Copyright 2014 Jonas Suhr Christensen + * jsc@umbraculum.org + ****************************************************************************/ + +/* + * This file is part of DrumGizmo. + * + * DrumGizmo is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * DrumGizmo is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with DrumGizmo; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ +#ifndef __DRUMGIZMO_CONFIG_H__ +#define __DRUMGIZMO_CONFIG_H__ + +#include +#include + +#define DIRECTORY_HIDDEN 1 + +class Config { + + public: + Config(); + ~Config(); + + void load(); + void save(); + + std::string lastkit; + std::string lastmidimap; +}; + +#endif/*__DRUMGIZMO_CONFIG_H__*/ diff --git a/plugingui/plugingui.cc b/plugingui/plugingui.cc index 6601be7..b235af3 100644 --- a/plugingui/plugingui.cc +++ b/plugingui/plugingui.cc @@ -33,6 +33,7 @@ #include "verticalline.h" #include "../version.h" +#include "pluginconfig.h" #include "messagehandler.h" static void checkClick(void *ptr) @@ -81,6 +82,12 @@ static void knobChange2(void *ptr) #endif } +//static void quit(void *ptr) { +// PluginGUI *gui = (PluginGUI*)ptr; +// +// gui->stopThread(); +//} + GUI::FileBrowser *fb; static void selectKitFile(void *ptr, std::string filename) { @@ -91,6 +98,8 @@ static void selectKitFile(void *ptr, std::string filename) std::string drumkit = gui->lineedit->text(); + gui->config->lastkit = drumkit; + gui->progress->setProgress(0); gui->progress->setState(GUI::ProgressBar::blue); @@ -105,6 +114,7 @@ static void kitBrowseClick(void *ptr) PluginGUI *gui = (PluginGUI*)ptr; std::string path = gui->lineedit->text(); + if(path == "") path = gui->config->lastkit; if(path == "") path = gui->lineedit2->text(); fb->setPath(path); @@ -121,6 +131,8 @@ static void selectMapFile(void *ptr, std::string filename) std::string midimap = gui->lineedit2->text(); + gui->config->lastmidimap = midimap; + LoadMidimapMessage *msg = new LoadMidimapMessage(); msg->midimapfile = midimap; msghandler.sendMessage(MSGRCV_ENGINE, msg); @@ -137,6 +149,7 @@ static void midimapBrowseClick(void *ptr) PluginGUI *gui = (PluginGUI*)ptr; std::string path = gui->lineedit2->text(); + if(path == "") path = gui->config->lastmidimap; if(path == "") path = gui->lineedit->text(); fb->setPath(path); @@ -268,6 +281,10 @@ void PluginGUI::thread_main() void PluginGUI::deinit() { + if(config) { + config->save(); + delete config; + } if(window) delete window; } @@ -280,6 +297,10 @@ void closeEventHandler(void *ptr) void PluginGUI::init() { DEBUG(gui, "init"); + + config = new Config(); + config->load(); + window = new GUI::Window(); window->eventHandler()->registerCloseHandler(closeEventHandler, (void*)&closing); @@ -428,6 +449,13 @@ void PluginGUI::init() filebrowser->hide(); fb = filebrowser; + // Enable quit button +// GUI::Button *btn_quit = new GUI::Button(window); +// btn_quit->setText("Quit"); +// btn_quit->move(50,280); +// btn_quit->resize(80,80); +// btn_quit->registerClickHandler(quit, this); + window->show(); sem.post(); diff --git a/plugingui/plugingui.h b/plugingui/plugingui.h index 7ec5e77..f94a062 100644 --- a/plugingui/plugingui.h +++ b/plugingui/plugingui.h @@ -36,6 +36,7 @@ #include "button.h" #include "knob.h" #include "progressbar.h" +#include "pluginconfig.h" #include "filebrowser.h" @@ -80,6 +81,8 @@ public: GUI::LineEdit *lineedit2; GUI::ProgressBar *progress2; + Config *config; + void (*windowClosedHandler)(void *); void *windowClosedPtr; -- cgit v1.2.3