summaryrefslogtreecommitdiffstats
path: root/src/ui
diff options
context:
space:
mode:
authorMartin Owens <doctormo@gmail.com>2017-07-02 14:47:03 +0000
committerMartin Owens <doctormo@gmail.com>2017-07-02 14:47:03 +0000
commit7dc5626af3eb924fa381c46491cd49178a78cd73 (patch)
treee50a3b98ce1393b7a06efbd11425f8c458b0cbbb /src/ui
parentpyc files are generated when testing modules, ignore them (diff)
downloadinkscape-7dc5626af3eb924fa381c46491cd49178a78cd73.tar.gz
inkscape-7dc5626af3eb924fa381c46491cd49178a78cd73.zip
Move directory scanners to new get_filenames(...) calls
Diffstat (limited to 'src/ui')
-rw-r--r--src/ui/dialog/swatches.cpp59
-rw-r--r--src/ui/dialog/symbols.cpp97
-rw-r--r--src/ui/dialog/template-load-tab.cpp87
3 files changed, 73 insertions, 170 deletions
diff --git a/src/ui/dialog/swatches.cpp b/src/ui/dialog/swatches.cpp
index 82b099bd1..28269c81b 100644
--- a/src/ui/dialog/swatches.cpp
+++ b/src/ui/dialog/swatches.cpp
@@ -24,6 +24,8 @@
#include <glibmm/i18n.h>
#include <glibmm/main.h>
#include <glibmm/timer.h>
+#include <glibmm/fileutils.h>
+#include <glibmm/miscutils.h>
#include "color-item.h"
#include "desktop.h"
@@ -384,10 +386,11 @@ static bool parseNum( char*& str, int& val ) {
}
-void _loadPaletteFile( gchar const *filename, gchar const *path, gboolean user/*=FALSE*/ )
+void _loadPaletteFile(Glib::ustring path, gboolean user/*=FALSE*/)
{
+ Glib::ustring filename = Glib::path_get_basename(path);
char block[1024];
- FILE *f = Inkscape::IO::fopen_utf8name(path, "r" );
+ FILE *f = Inkscape::IO::fopen_utf8name(path.c_str(), "r");
if ( f ) {
char* result = fgets( block, sizeof(block), f );
if ( result ) {
@@ -396,7 +399,7 @@ void _loadPaletteFile( gchar const *filename, gchar const *path, gboolean user/*
bool hasErr = false;
SwatchPage *onceMore = new SwatchPage();
- onceMore->_name = filename;
+ onceMore->_name = filename.c_str();
do {
result = fgets( block, sizeof(block), f );
@@ -518,56 +521,18 @@ static void loadEmUp()
if ( !beenHere ) {
beenHere = true;
- std::list<gchar *> sources;
- sources.push_back(g_strdup(get_path(USER, PALETTES)));
- sources.push_back(g_strdup(get_path(SYSTEM, PALETTES)));
- sources.push_back(g_strdup(get_path(CREATE, PALETTES)));
-
- // Use this loop to iterate through a list of possible document locations.
- while (!sources.empty()) {
- gchar *dirname = sources.front();
- if ( Inkscape::IO::file_test( dirname, G_FILE_TEST_EXISTS )
- && Inkscape::IO::file_test( dirname, G_FILE_TEST_IS_DIR )) {
- GError *err = 0;
- GDir *directory = g_dir_open(dirname, 0, &err);
- if (!directory) {
- gchar *safeDir = Inkscape::IO::sanitizeString(dirname);
- g_warning(_("Palettes directory (%s) is unavailable."), safeDir);
- g_free(safeDir);
- } else {
- gchar *filename = 0;
- while ((filename = (gchar *)g_dir_read_name(directory)) != NULL) {
- gchar* lower = g_ascii_strdown( filename, -1 );
-// if ( g_str_has_suffix(lower, ".gpl") ) {
- if ( !g_str_has_suffix(lower, "~") ) {
- gchar* full = g_build_filename(dirname, filename, NULL);
- if ( !Inkscape::IO::file_test( full, G_FILE_TEST_IS_DIR ) ) {
- _loadPaletteFile(filename, full, userPalette);
- }
- g_free(full);
- }
-// }
- g_free(lower);
- }
- g_dir_close(directory);
- }
- }
-
- // toss the dirname
- g_free(dirname);
- sources.pop_front();
- userPalette = false;
+ for(auto &filename: get_filenames(PALETTES, {".gpl"})) {
+ bool userPalette = Inkscape::IO::file_is_writable(filename.c_str());
+ _loadPaletteFile(filename, userPalette);
}
}
- // Sort the list of swatches by name, grouped by user/system
- userSwatchPages.sort(compare_swatch_names);
- systemSwatchPages.sort(compare_swatch_names);
-
+ // Sort the list of swatches by name, grouped by user/system
+ userSwatchPages.sort(compare_swatch_names);
+ systemSwatchPages.sort(compare_swatch_names);
}
-
SwatchesPanel& SwatchesPanel::getInstance()
{
return *new SwatchesPanel();
diff --git a/src/ui/dialog/symbols.cpp b/src/ui/dialog/symbols.cpp
index aa3d86b00..96ccf416b 100644
--- a/src/ui/dialog/symbols.cpp
+++ b/src/ui/dialog/symbols.cpp
@@ -495,20 +495,19 @@ class REVENGE_API RVNGSVGDrawingGenerator_WithTitle : public RVNGSVGDrawingGener
#endif
// Read Visio stencil files
-SPDocument* read_vss( gchar* fullname, Glib::ustring name ) {
-
+SPDocument* read_vss(Glib::ustring filename, Glib::ustring name ) {
+ gchar *fullname;
#ifdef WIN32
// RVNGFileStream uses fopen() internally which unfortunately only uses ANSI encoding on Windows
// therefore attempt to convert uri to the system codepage
// even if this is not possible the alternate short (8.3) file name will be used if available
- fullname = g_win32_locale_filename_from_utf8(fullname);
+ fullname = g_win32_locale_filename_from_utf8(fullname.c_str());
+ #else
+ filename.copy(fullname, filename.length());
#endif
RVNGFileStream input(fullname);
-
- #ifdef WIN32
- g_free(fullname);
- #endif
+ g_free(fullname);
if (!libvisio::VisioDocument::isSupported(&input)) {
return NULL;
@@ -590,74 +589,32 @@ SPDocument* read_vss( gchar* fullname, Glib::ustring name ) {
/* Hunts preference directories for symbol files */
void SymbolsDialog::get_symbols() {
- std::list<Glib::ustring> directories;
-
- using namespace Inkscape::IO::Resource;
- directories.push_back(get_path_ustring(USER, SYMBOLS));
- directories.push_back(get_path_ustring(SYSTEM, SYMBOLS));
-
- std::list<Glib::ustring>::iterator it;
- for( it = directories.begin(); it != directories.end(); ++it ) {
- if(!Inkscape::IO::file_test((*it).c_str(), G_FILE_TEST_IS_DIR)) {
- continue;
- }
-
- GError *err = 0;
- GDir *dir = g_dir_open( (*it).c_str(), 0, &err );
- if( dir ) {
-
- gchar *filename = 0;
- while( (filename = (gchar *)g_dir_read_name( dir ) ) != NULL) {
-
- gchar *fullname = g_build_filename((*it).c_str(), filename, NULL);
-
- if ( !Inkscape::IO::file_test( fullname, G_FILE_TEST_IS_DIR )
- && ( Glib::str_has_suffix(fullname, ".svg") || Glib::str_has_suffix(fullname, ".vss") ) ) {
-
- Glib::ustring fn( filename );
- Glib::ustring tag = fn.substr( fn.find_last_of(".") + 1 );
-
- SPDocument* symbol_doc = NULL;
+ using namespace Inkscape::IO::Resource;
+ SPDocument* symbol_doc = NULL;
+ Glib::ustring title;
+
+ for(auto &filename: get_filenames(SYMBOLS, {".svg", ".vss"})) {
+ if(Glib::str_has_suffix(filename, ".svg")) {
+ symbol_doc = SPDocument::createNewDoc(filename.c_str(), FALSE);
+ if(symbol_doc) {
+ title = symbol_doc->getRoot()->title();
+ if(title.empty()) {
+ title = _("Unnamed Symbols");
+ }
+ }
+ }
#ifdef WITH_LIBVISIO
- if( tag.compare( "vss" ) == 0 ) {
- // strip extension from filename and use it as name for the symbol set
- Glib::ustring name = Glib::ustring(filename);
- name = name.erase(name.rfind('.'));
-
- symbol_doc = read_vss( fullname, name );
- if( symbol_doc ) {
- symbolSets[name]= symbol_doc;
- symbolSet->append(name);
- }
- }
+ if(Glib::str_has_suffix(filename, ".vss")) {
+ Glib::ustring title = filename.erase(filename.rfind('.'));
+ symbol_doc = read_vss(filename, title);
+ }
#endif
- // Try to read all remaining files as SVG
- if( !symbol_doc ) {
-
- symbol_doc = SPDocument::createNewDoc( fullname, FALSE );
- if( symbol_doc ) {
-
- const gchar *title = symbol_doc->getRoot()->title();
-
- // A user provided file may not have a title
- if( title != NULL ) {
- title = g_dpgettext2(NULL, "Symbol", title); // Translate
- } else {
- title = _("Unnamed Symbols");
- }
-
- symbolSets[Glib::ustring(title)] = symbol_doc;
- symbolSet->append(title);
- }
- }
-
+ if(symbol_doc) {
+ symbolSets[title]= symbol_doc;
+ symbolSet->append(title);
}
- g_free( fullname );
- }
- g_dir_close( dir );
}
- }
}
GSList* SymbolsDialog::symbols_in_doc_recursive (SPObject *r, GSList *l)
diff --git a/src/ui/dialog/template-load-tab.cpp b/src/ui/dialog/template-load-tab.cpp
index 10b4716ff..7dd889b2f 100644
--- a/src/ui/dialog/template-load-tab.cpp
+++ b/src/ui/dialog/template-load-tab.cpp
@@ -2,7 +2,7 @@
* @brief New From Template abstract tab implementation
*/
/* Authors:
- * Jan Darowski <jan.darowski@gmail.com>, supervised by Krzysztof Kosiński
+ * Jan Darowski <jan.darowski@gmail.com>, supervised by Krzysztof Kosiński
*
* Copyright (C) 2013 Authors
* Released under GNU GPL, read the file 'COPYING' for more information
@@ -38,27 +38,27 @@ TemplateLoadTab::TemplateLoadTab(NewFromTemplate* parent)
set_border_width(10);
_info_widget = Gtk::manage(new TemplateWidget());
-
+
Gtk::Label *title;
title = Gtk::manage(new Gtk::Label(_("Search:")));
_search_box.pack_start(*title, Gtk::PACK_SHRINK);
_search_box.pack_start(_keywords_combo, Gtk::PACK_SHRINK, 5);
-
+
_tlist_box.pack_start(_search_box, Gtk::PACK_SHRINK, 10);
-
+
pack_start(_tlist_box, Gtk::PACK_SHRINK);
pack_start(*_info_widget, Gtk::PACK_EXPAND_WIDGET, 5);
-
+
Gtk::ScrolledWindow *scrolled;
scrolled = Gtk::manage(new Gtk::ScrolledWindow());
scrolled->set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
scrolled->add(_tlist_view);
_tlist_box.pack_start(*scrolled, Gtk::PACK_EXPAND_WIDGET, 5);
-
+
_keywords_combo.signal_changed().connect(
sigc::mem_fun(*this, &TemplateLoadTab::_keywordSelected));
this->show_all();
-
+
_loadTemplates();
_initLists();
}
@@ -91,14 +91,14 @@ void TemplateLoadTab::_displayTemplateInfo()
_info_widget->display(_tdata[_current_template]);
_parent_widget->setCreateButtonSensitive(true);
}
-
+
}
void TemplateLoadTab::_initKeywordsList()
{
_keywords_combo.append(_("All"));
-
+
for (std::set<Glib::ustring>::iterator it = _keywords.begin() ; it != _keywords.end() ; ++it){
_keywords_combo.append(*it);
}
@@ -111,15 +111,15 @@ void TemplateLoadTab::_initLists()
_tlist_view.set_model(_tlist_store);
_tlist_view.append_column("", _columns.textValue);
_tlist_view.set_headers_visible(false);
-
+
_initKeywordsList();
_refreshTemplatesList();
-
+
Glib::RefPtr<Gtk::TreeSelection> templateSelectionRef =
- _tlist_view.get_selection();
+ _tlist_view.get_selection();
templateSelectionRef->signal_changed().connect(
sigc::mem_fun(*this, &TemplateLoadTab::_displayTemplateInfo));
-
+
_tlist_view.signal_row_activated().connect(
sigc::mem_fun(*this, &TemplateLoadTab::_onRowActivated));
}
@@ -133,10 +133,10 @@ void TemplateLoadTab::_keywordSelected()
}
else
_current_search_type = LIST_KEYWORD;
-
+
if (_current_keyword == "" || _current_keyword == _("All"))
_current_search_type = ALL;
-
+
_refreshTemplatesList();
}
@@ -203,14 +203,17 @@ void TemplateLoadTab::_refreshTemplatesList()
_info_widget->clear();
_parent_widget->setCreateButtonSensitive(false);
}
-}
+}
void TemplateLoadTab::_loadTemplates()
{
- _getTemplatesFromDomain(USER);
- _getTemplatesFromDomain(SYSTEM);
-
+ for(auto &filename: get_filenames(TEMPLATES, {".svg"}, {"default.", "default_"})) {
+ TemplateData tmp = _processTemplateFile(filename);
+ if (tmp.display_name != "")
+ _tdata[tmp.display_name] = tmp;
+
+ }
// procedural templates
_getProceduralTemplates();
}
@@ -222,73 +225,51 @@ TemplateLoadTab::TemplateData TemplateLoadTab::_processTemplateFile(const std::s
result.path = path;
result.is_procedural = false;
result.preview_name = "";
-
+
// convert path into valid template name
result.display_name = Glib::path_get_basename(path);
gsize n = 0;
while ((n = result.display_name.find_first_of("_", 0)) < Glib::ustring::npos){
result.display_name.replace(n, 1, 1, ' ');
- }
+ }
n = result.display_name.rfind(".svg");
result.display_name.replace(n, 4, 1, ' ');
-
+
Inkscape::XML::Document *rdoc = sp_repr_read_file(path.data(), SP_SVG_NS_URI);
if (rdoc){
Inkscape::XML::Node *myRoot = rdoc->root();
if (strcmp(myRoot->name(), "svg:svg") != 0){ // Wrong file format
return result;
}
-
+
myRoot = sp_repr_lookup_name(myRoot, "inkscape:_templateinfo");
-
+
if (myRoot == NULL) // No template info
return result;
_getDataFromNode(myRoot, result);
}
-
- return result;
-}
-
-void TemplateLoadTab::_getTemplatesFromDomain(Domain domain)
-{
- Glib::ustring path = get_path_ustring(domain, TEMPLATES);
- if (!Glib::file_test(path, Glib::FILE_TEST_IS_DIR)) {
- return;
- }
-
- Glib::Dir dir(path);
-
- std::string file = Glib::build_filename(path, dir.read_name());
- while (file != path){
- if (Glib::str_has_suffix(file, ".svg") && !Glib::str_has_prefix(Glib::path_get_basename(file), "default.")){
- TemplateData tmp = _processTemplateFile(file);
- if (tmp.display_name != "")
- _tdata[tmp.display_name] = tmp;
- }
- file = Glib::build_filename(path, dir.read_name());
- }
+ return result;
}
-
void TemplateLoadTab::_getProceduralTemplates()
{
std::list<Inkscape::Extension::Effect *> effects;
Inkscape::Extension::db.get_effect_list(effects);
-
+
std::list<Inkscape::Extension::Effect *>::iterator it = effects.begin();
while (it != effects.end()){
Inkscape::XML::Node *myRoot;
myRoot = (*it)->get_repr();
myRoot = sp_repr_lookup_name(myRoot, "inkscape:_templateinfo");
-
+
if (myRoot){
TemplateData result;
result.display_name = (*it)->get_name();
result.is_procedural = true;
result.path = "";
result.tpl_effect = *it;
-
+
_getDataFromNode(myRoot, result);
_tdata[result.display_name] = result;
}
@@ -312,18 +293,18 @@ void TemplateLoadTab::_getDataFromNode(Inkscape::XML::Node *dataNode, TemplateDa
data.preview_name = currentData->firstChild()->content();
if ((currentData = sp_repr_lookup_name(dataNode, "inkscape:date")) != NULL)
data.creation_date = currentData->firstChild()->content();
-
+
if ((currentData = sp_repr_lookup_name(dataNode, "inkscape:_keywords")) != NULL){
Glib::ustring tplKeywords = _(currentData->firstChild()->content());
while (!tplKeywords.empty()){
std::size_t pos = tplKeywords.find_first_of(" ");
if (pos == Glib::ustring::npos)
pos = tplKeywords.size();
-
+
Glib::ustring keyword = tplKeywords.substr(0, pos).data();
data.keywords.insert(keyword.lowercase());
_keywords.insert(keyword.lowercase());
-
+
if (pos == tplKeywords.size())
break;
tplKeywords.erase(0, pos+1);