template-load-tab.cpp revision aea39058a556f160194bf12f84ee7254af96278d
0N/A/** @file
1949N/A * @brief New From Template abstract tab implementation
0N/A */
0N/A/* Authors:
0N/A * Jan Darowski <jan.darowski@gmail.com>, supervised by Krzysztof KosiƄski
0N/A *
0N/A * Copyright (C) 2013 Authors
0N/A * Released under GNU GPL, read the file 'COPYING' for more information
0N/A */
0N/A
0N/A#include "template-load-tab.h"
0N/A#include "template-widget.h"
0N/A
0N/A#include <gtkmm/messagedialog.h>
0N/A#include <gtkmm/scrolledwindow.h>
0N/A#include <glibmm/i18n.h>
0N/A#include <iostream>
0N/A
1472N/A#include "interface.h"
1472N/A#include "file.h"
1472N/A#include "path-prefix.h"
0N/A#include "preferences.h"
0N/A#include "inkscape.h"
0N/A#include "xml/repr.h"
0N/A#include "xml/document.h"
0N/A#include "xml/node.h"
0N/A
0N/A
0N/Anamespace Inkscape {
0N/Anamespace UI {
0N/A
0N/A
0N/ATemplateLoadTab::TemplateLoadTab()
0N/A : _current_keyword("")
0N/A , _keywords_combo(true)
0N/A , _current_search_type(ALL)
0N/A{
0N/A set_border_width(10);
0N/A
1010N/A _info_widget = manage(new TemplateWidget());
1010N/A
1010N/A Gtk::Label *title;
1010N/A title = manage(new Gtk::Label(_("Search:")));
1010N/A _search_box.pack_start(*title, Gtk::PACK_SHRINK);
1010N/A _search_box.pack_start(_keywords_combo, Gtk::PACK_SHRINK, 5);
1010N/A
1010N/A _tlist_box.pack_start(_search_box, Gtk::PACK_SHRINK, 10);
1010N/A
1010N/A pack_start(_tlist_box, Gtk::PACK_SHRINK);
1010N/A pack_start(*_info_widget, Gtk::PACK_EXPAND_WIDGET, 5);
0N/A
0N/A Gtk::ScrolledWindow *scrolled;
0N/A scrolled = manage(new Gtk::ScrolledWindow());
0N/A scrolled->set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
0N/A scrolled->add(_tlist_view);
0N/A _tlist_box.pack_start(*scrolled, Gtk::PACK_EXPAND_WIDGET, 5);
0N/A
0N/A _keywords_combo.signal_changed().connect(
0N/A sigc::mem_fun(*this, &TemplateLoadTab::_keywordSelected));
0N/A this->show_all();
0N/A
0N/A _loading_path = "";
0N/A _loadTemplates();
0N/A _initLists();
0N/A}
0N/A
0N/A
0N/ATemplateLoadTab::~TemplateLoadTab()
0N/A{
0N/A}
0N/A
0N/A
0N/Avoid TemplateLoadTab::createTemplate()
0N/A{
0N/A _info_widget->create();
0N/A}
0N/A
0N/A
0N/Avoid TemplateLoadTab::_displayTemplateInfo()
0N/A{
0N/A Glib::RefPtr<Gtk::TreeSelection> templateSelectionRef = _tlist_view.get_selection();
0N/A if (templateSelectionRef->get_selected()) {
0N/A _current_template = (*templateSelectionRef->get_selected())[_columns.textValue];
0N/A
0N/A _info_widget->display(_tdata[_current_template]);
0N/A }
0N/A
0N/A}
0N/A
0N/A
0N/Avoid TemplateLoadTab::_initKeywordsList()
0N/A{
0N/A _keywords_combo.append(_("All"));
0N/A
0N/A for (std::set<Glib::ustring>::iterator it = _keywords.begin() ; it != _keywords.end() ; ++it){
0N/A _keywords_combo.append(*it);
0N/A }
0N/A}
0N/A
0N/A
1601N/Avoid TemplateLoadTab::_initLists()
1601N/A{
1601N/A _tlist_store = Gtk::ListStore::create(_columns);
1601N/A _tlist_view.set_model(_tlist_store);
1601N/A _tlist_view.append_column("", _columns.textValue);
1601N/A _tlist_view.set_headers_visible(false);
1601N/A
1601N/A _initKeywordsList();
1601N/A _refreshTemplatesList();
1601N/A
1601N/A Glib::RefPtr<Gtk::TreeSelection> templateSelectionRef =
1601N/A _tlist_view.get_selection();
1601N/A templateSelectionRef->signal_changed().connect(
1601N/A sigc::mem_fun(*this, &TemplateLoadTab::_displayTemplateInfo));
1601N/A}
1601N/A
0N/A
0N/Avoid TemplateLoadTab::_keywordSelected()
0N/A{
0N/A _current_keyword = _keywords_combo.get_active_text();
0N/A if (_current_keyword == ""){
0N/A _current_keyword = _keywords_combo.get_entry_text();
1015N/A _current_search_type = USER_SPECIFIED;
1015N/A }
1015N/A else
1601N/A _current_search_type = LIST_KEYWORD;
2671N/A
1015N/A if (_current_keyword == "" || _current_keyword == _("All"))
1601N/A _current_search_type = ALL;
0N/A
0N/A _refreshTemplatesList();
1601N/A}
1601N/A
1010N/A
1010N/Avoid TemplateLoadTab::_refreshTemplatesList()
1010N/A{
1010N/A _tlist_store->clear();
1010N/A
0N/A switch (_current_search_type){
1601N/A case ALL :{
1601N/A
1601N/A for (std::map<Glib::ustring, TemplateData>::iterator it = _tdata.begin() ; it != _tdata.end() ; ++it) {
1601N/A Gtk::TreeModel::iterator iter = _tlist_store->append();
1601N/A Gtk::TreeModel::Row row = *iter;
1601N/A row[_columns.textValue] = it->first;
1601N/A }
1601N/A break;
1601N/A }
1601N/A
1601N/A case LIST_KEYWORD: {
1601N/A for (std::map<Glib::ustring, TemplateData>::iterator it = _tdata.begin() ; it != _tdata.end() ; ++it) {
1601N/A if (it->second.keywords.count(_current_keyword) != 0){
1601N/A Gtk::TreeModel::iterator iter = _tlist_store->append();
1601N/A Gtk::TreeModel::Row row = *iter;
row[_columns.textValue] = it->first;
}
}
break;
}
case USER_SPECIFIED : {
for (std::map<Glib::ustring, TemplateData>::iterator it = _tdata.begin() ; it != _tdata.end() ; ++it) {
if (it->second.keywords.count(_current_keyword) != 0 ||
it->second.display_name.find(_current_keyword) != Glib::ustring::npos ||
it->second.author.find(_current_keyword) != Glib::ustring::npos ||
it->second.short_description.find(_current_keyword) != Glib::ustring::npos ||
it->second.long_description.find(_current_keyword) != Glib::ustring::npos )
{
Gtk::TreeModel::iterator iter = _tlist_store->append();
Gtk::TreeModel::Row row = *iter;
row[_columns.textValue] = it->first;
}
}
break;
}
}
}
void TemplateLoadTab::_loadTemplates()
{
// user's local dir
_getTemplatesFromDir(profile_path("templates") + _loading_path);
// system templates dir
_getTemplatesFromDir(INKSCAPE_TEMPLATESDIR + _loading_path);
}
TemplateLoadTab::TemplateData TemplateLoadTab::_processTemplateFile(const Glib::ustring &path)
{
TemplateData result;
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;
rdoc = sp_repr_read_file(path.data(), SP_SVG_NS_URI);
Inkscape::XML::Node *myRoot;
Inkscape::XML::Node *dataNode;
std::cerr << path.c_str();
if (rdoc){
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;
if ((dataNode = sp_repr_lookup_name(myRoot, "inkscape:_name")) != NULL)
result.display_name = dgettext("Document template name", dataNode->firstChild()->content());
if ((dataNode = sp_repr_lookup_name(myRoot, "inkscape:author")) != NULL)
result.author = dataNode->firstChild()->content();
if ((dataNode = sp_repr_lookup_name(myRoot, "inkscape:_short")) != NULL)
result.short_description = dgettext("Document template short description", dataNode->firstChild()->content());
if ((dataNode = sp_repr_lookup_name(myRoot, "inkscape:_long") )!= NULL)
result.long_description = dgettext("Document template long description", dataNode->firstChild()->content());
if ((dataNode = sp_repr_lookup_name(myRoot, "inkscape:preview")) != NULL)
result.preview_name = dataNode->firstChild()->content();
if ((dataNode = sp_repr_lookup_name(myRoot, "inkscape:date")) != NULL){
result.creation_date = dataNode->firstChild()->content();
}
if ((dataNode = sp_repr_lookup_name(myRoot, "inkscape:_keywords")) != NULL){
Glib::ustring data = dataNode->firstChild()->content();
while (!data.empty()){
int pos = data.find_first_of(" ");
if (pos == Glib::ustring::npos)
pos = data.size();
Glib::ustring keyword = dgettext("Document template keyword", data.substr(0, pos).data());
result.keywords.insert(keyword);
_keywords.insert(keyword);
if (pos == data.size())
break;
data.erase(0, pos+1);
}
}
}
return result;
}
void TemplateLoadTab::_getTemplatesFromDir(const Glib::ustring &path)
{
if ( !Glib::file_test(path, Glib::FILE_TEST_EXISTS) ||
!Glib::file_test(path, Glib::FILE_TEST_IS_DIR))
return;
Glib::Dir dir(path);
Glib::ustring 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());
}
}
}
}