1 /*
2  * Copyright © 2004-2010 Jens Oknelid, paskharen@gmail.com
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  *
18  * In addition, as a special exception, compiling, linking, and/or
19  * using OpenSSL with this program is allowed.
20  */
21 
22 #include "entry.hh"
23 #include <dcpp/stdinc.h>
24 #include <dcpp/Util.h>
25 #include "wulformanager.hh"
26 
27 using namespace std;
28 
Entry(const EntryType type,const string & ui,const string & id)29 Entry::Entry(const EntryType type, const string &ui, const string &id):
30     xml(NULL),
31     type(type),
32     id(dcpp::Util::toString(type) + ":" + id)
33 {
34     // Load the GtkBuilder XML file, if applicable
35     if (!ui.empty())
36     {
37         string file = WulforManager::get()->getPath() + "/ui/" + ui;
38         GError *error = NULL;
39         xml = gtk_builder_new();
40         gtk_builder_add_from_file(xml,file.c_str(),&error);
41         if (error)
42         {
43             g_print("GTKBUILDER ERROR file => %s ,\n => %s",file.c_str(),error->message);
44             gtk_main_quit();
45         }
46     }
47 }
48 
~Entry()49 Entry::~Entry()
50 {
51 
52 }
53 
getType()54 const Entry::EntryType Entry::getType()
55 {
56     return type;
57 }
58 
getID()59 const string& Entry::getID()
60 {
61     return id;
62 }
63 
remove()64 void Entry::remove()
65 {
66     removeChildren();
67     WulforManager::get()->deleteEntry_gui(this);
68 }
69 
70 /*
71  * Generates a unique ID to allow for duplicate entries
72  */
generateID()73 string Entry::generateID()
74 {
75     return dcpp::Util::toString((long)this);
76 }
77 
getWidget(const string & name)78 GtkWidget *Entry::getWidget(const string &name)
79 {
80     dcassert(xml && !name.empty());
81     GtkWidget *widget = GTK_WIDGET(gtk_builder_get_object(xml,name.c_str()));
82     dcassert(widget);
83     return widget;
84 }
85 
addChild(Entry * entry)86 void Entry::addChild(Entry *entry)
87 {
88     children.insert(make_pair(entry->getID(), entry));
89     WulforManager::get()->insertEntry_gui(entry);
90 }
91 
getChild(const EntryType childType,const string & childId)92 Entry *Entry::getChild(const EntryType childType, const string &childId)
93 {
94     string id = dcpp::Util::toString(childType) + ":" + childId;
95     auto it = children.find(id);
96 
97     if (it == children.end())
98         return NULL;
99     else
100         return it->second;
101 }
102 
removeChild(const EntryType childType,const string & childId)103 void Entry::removeChild(const EntryType childType, const string &childId)
104 {
105     Entry *entry = getChild(childType, childId);
106     removeChild(entry);
107 }
108 
removeChild(Entry * entry)109 void Entry::removeChild(Entry *entry)
110 {
111     if (entry)
112     {
113         entry->removeChildren();
114         children.erase(entry->getID());
115         WulforManager::get()->deleteEntry_gui(entry);
116     }
117 }
118 
removeChildren()119 void Entry::removeChildren()
120 {
121     while (!children.empty())
122         removeChild(children.begin()->second);
123 }
124