Review Kademlia assertions
[amule.git] / src / CatDialog.cpp
blob9232b1afed335714158c7976e5e38cc3ddd50d77
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2003-2011 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) created by Ornis
6 //
7 // Any parts of this program derived from the xMule, lMule or eMule project,
8 // or contributed by third-party developers are copyrighted by their
9 // respective authors.
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include <wx/wx.h>
27 #include <wx/colordlg.h>
29 #ifdef __WINDOWS__
30 #include <io.h> // Do_not_auto_remove
31 #endif
33 #include "CatDialog.h" // Interface declarations.
34 #include "DownloadListCtrl.h" // Needed for CDownloadListCtrl
35 #include "TransferWnd.h" // Needed for CTransferWnd
36 #include "amuleDlg.h" // Needed for CamuleDlg
37 #include "SearchDlg.h" // Needed for UpdateCatChoice
38 #include <common/StringFunctions.h> // Needed for MakeFoldername
39 #include "OtherFunctions.h" // Needed for CastChild
40 #include "Preferences.h" // Needed for CPreferences
41 #include "amule.h" // Needed for theApp
42 #include "muuli_wdr.h" // Needed for CategoriesEditWindow
45 BEGIN_EVENT_TABLE(CCatDialog,wxDialog)
46 EVT_BUTTON(wxID_OK, CCatDialog::OnBnClickedOk)
47 EVT_BUTTON(IDC_CATCOLOR, CCatDialog::OnBnClickColor)
48 EVT_BUTTON(IDC_BROWSE, CCatDialog::OnBnClickedBrowse)
49 END_EVENT_TABLE()
52 * This class is used in both amule and amulegui. It cannot go into
53 * libmuleappgui because of preferences. When the compiler must passes
54 * here on the remote client, the variable glob_prefs has two different
55 * types, in one case it is (CPreferences *), on the other case it is
56 * (CPreferencesRem *). A proper fix involves a class hierarchy redesign.
58 CCatDialog::CCatDialog(wxWindow* parent, bool allowbrowse, int index)
60 wxDialog(parent, -1, _("Category"),
61 wxDefaultPosition, wxDefaultSize,
62 wxDEFAULT_DIALOG_STYLE|wxSYSTEM_MENU)
64 wxSizer* content = CategoriesEditWindow(this, true);
65 content->Show(this, true);
66 Center();
67 m_category = NULL;
69 // Attempt to get the specified category, this may or may not succeed,
70 // we dont really care. If it fails (too high index or such), then we
71 // simply get NULL and create a new category
72 if (index > -1) {
73 m_category = theApp->glob_prefs->GetCategory(index);
76 if (m_category) {
77 // Filling values by the specified category
78 CastChild(IDC_TITLE, wxTextCtrl)->SetValue(m_category->title);
79 // We use the 'raw' filename, since the value is also passed to wxDirSelector
80 CastChild(IDC_INCOMING, wxTextCtrl)->SetValue(m_category->path.GetRaw());
81 CastChild(IDC_COMMENT, wxTextCtrl)->SetValue(m_category->comment);
82 CastChild(IDC_PRIOCOMBO,wxChoice)->SetSelection(m_category->prio);
84 m_colour = CMuleColour(m_category->color);
85 } else {
86 // Default values for new categories
87 CastChild(IDC_TITLE, wxTextCtrl)->SetValue(_("New Category"));
88 CastChild(IDC_INCOMING, wxTextCtrl)->SetValue(thePrefs::GetIncomingDir().GetRaw());
89 CastChild(IDC_COMMENT, wxTextCtrl)->SetValue(wxEmptyString);
90 CastChild(IDC_PRIOCOMBO,wxChoice)->SetSelection(0);
92 m_colour = CMuleColour(rand() % 255, rand() % 255, rand() % 255);
95 CastChild(ID_BOX_CATCOLOR, wxStaticBitmap)->SetBitmap(MakeBitmap());
97 if (!allowbrowse) {
98 CastChild(IDC_BROWSE, wxButton)->Destroy();
103 CCatDialog::~CCatDialog()
108 wxBitmap CCatDialog::MakeBitmap()
110 wxBitmap bitmap(16, 16);
111 wxMemoryDC dc(bitmap);
113 dc.SetBrush(m_colour.GetBrush());
114 dc.DrawRectangle(0, 0, 16, 16);
116 return bitmap;
120 void CCatDialog::OnBnClickedBrowse(wxCommandEvent& WXUNUSED(evt))
122 wxString dir = CastChild(IDC_INCOMING, wxTextCtrl)->GetValue();
124 dir = wxDirSelector(
125 _("Choose a folder for incoming files"),
126 dir, wxDD_DEFAULT_STYLE, wxDefaultPosition, this);
127 if (!dir.IsEmpty()) {
128 CastChild(IDC_INCOMING, wxTextCtrl)->SetValue(dir);
133 void CCatDialog::OnBnClickedOk(wxCommandEvent& WXUNUSED(evt))
135 wxString newname = CastChild(IDC_TITLE, wxTextCtrl)->GetValue();
137 // No empty names
138 if (newname.IsEmpty()) {
139 wxMessageBox(
140 _("You must specify a name for the category!"),
141 _("Info"), wxOK, this);
142 return;
145 CPath newpath = CPath(CastChild(IDC_INCOMING, wxTextCtrl)->GetValue());
147 // No empty dirs please
148 if (!newpath.IsOk()) {
149 wxMessageBox(
150 _("You must specify a path for the category!"),
151 _("Info"), wxOK, this);
153 return;
156 // remote gui:
157 // Pass path unchecked (and don't try to create it on the wrong machine...).
158 // It will be checked on the server, and an error message created.
159 #ifndef CLIENT_GUI
160 if (!newpath.DirExists()) {
161 if (!CPath::MakeDir(newpath)) {
162 wxMessageBox(_("Failed to create incoming dir for category. Please specify a valid path!"),
163 _("Info"), wxOK, this);
164 return;
167 #endif
169 // Check if we are using an existing category, and if we are, if it has
170 // been removed in the mean-while. Otherwise create new category.
171 // lfroen: The only place where it could happen, is removing category
172 // from remote gui, while local gui amule have dialog opened in this
173 // category.
174 int index = -1;
175 if (m_category) {
176 // Check if the original category still exists
177 bool found = false;
178 for (uint32 i = 0; i < theApp->glob_prefs->GetCatCount(); ++i) {
179 if (m_category == theApp->glob_prefs->GetCategory(i)) {
180 found = true;
181 index = i;
182 break;
185 if (!found) {
186 m_category = 0;
190 if (!m_category) {
191 // New category, or the old one is gone
192 theApp->glob_prefs->CreateCategory(
193 m_category, newname, newpath,
194 CastChild(IDC_COMMENT, wxTextCtrl)->GetValue(),
195 m_colour.GetULong(),
196 CastChild(IDC_PRIOCOMBO, wxChoice)->GetSelection());
198 theApp->amuledlg->m_transferwnd->AddCategory(m_category);
199 } else {
200 theApp->glob_prefs->UpdateCategory(index, newname, newpath,
201 CastChild(IDC_COMMENT, wxTextCtrl)->GetValue(), m_colour.GetULong(),
202 CastChild(IDC_PRIOCOMBO, wxChoice)->GetSelection());
204 theApp->amuledlg->m_transferwnd->UpdateCategory(index);
205 theApp->amuledlg->m_transferwnd->downloadlistctrl->Refresh();
206 theApp->amuledlg->m_searchwnd->UpdateCatChoice();
209 EndModal(wxID_OK);
213 void CCatDialog::OnBnClickColor(wxCommandEvent& WXUNUSED(evt))
215 wxColour newcol = wxGetColourFromUser(this, m_colour);
216 if (newcol.Ok()) {
217 m_colour = newcol;
218 CastChild(ID_BOX_CATCOLOR, wxStaticBitmap)->SetBitmap(MakeBitmap());
221 // File_checked_for_headers