Upstream tarball 20080929
[amule.git] / src / CatDialog.cpp
blob026b5062cf2d0a08ca5944b4754ddda9f9ba47d1
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2003-2008 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.
20 //
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 __WXMSW__
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 if (!newpath.DirExists()) {
157 if (!CPath::MakeDir(newpath)) {
158 wxMessageBox(_("Failed to create incoming dir for category. Please specify a valid path!"),
159 _("Info"), wxOK, this);
160 return;
164 // Check if we are using an existing category, and if we are, if it has
165 // been removed in the mean-while. Otherwise create new category.
166 // lfroen: The only place where it could happen, is removing category
167 // from remote gui, while local gui amule have dialog opened in this
168 // category.
169 int index = -1;
170 if (m_category) {
171 // Check if the original category still exists
172 bool found = false;
173 for (uint32 i = 0; i < theApp->glob_prefs->GetCatCount(); ++i) {
174 if (m_category == theApp->glob_prefs->GetCategory(i)) {
175 found = true;
176 index = i;
177 break;
180 if (!found) {
181 m_category = 0;
185 if (!m_category) {
186 // New category, or the old one is gone
187 m_category = theApp->glob_prefs->CreateCategory(
188 newname, newpath,
189 CastChild(IDC_COMMENT, wxTextCtrl)->GetValue(),
190 m_colour.GetULong(),
191 CastChild(IDC_PRIOCOMBO, wxChoice)->GetSelection());
193 theApp->amuledlg->m_transferwnd->AddCategory(m_category);
194 } else {
195 theApp->glob_prefs->UpdateCategory(index, newname, newpath,
196 CastChild(IDC_COMMENT, wxTextCtrl)->GetValue(), m_colour.GetULong(),
197 CastChild(IDC_PRIOCOMBO, wxChoice)->GetSelection());
199 theApp->amuledlg->m_transferwnd->UpdateCategory(index);
200 theApp->amuledlg->m_transferwnd->downloadlistctrl->Refresh();
201 theApp->amuledlg->m_searchwnd->UpdateCatChoice();
204 EndModal(wxID_OK);
208 void CCatDialog::OnBnClickColor(wxCommandEvent& WXUNUSED(evt))
210 wxColour newcol = wxGetColourFromUser(this, m_colour);
211 if (newcol.Ok()) {
212 m_colour = newcol;
213 CastChild(ID_BOX_CATCOLOR, wxStaticBitmap)->SetBitmap(MakeBitmap());
216 // File_checked_for_headers