1 //////////////////////////////////////////////////////////////////////////
2 //
3 // pgAdmin III - PostgreSQL Tools
4 //
5 // Copyright (C) 2002 - 2016, The pgAdmin Development Team
6 // This software is released under the PostgreSQL Licence
7 //
8 // dlgManageFavourites.cpp - Manage favourites
9 //
10 //////////////////////////////////////////////////////////////////////////
11
12
13
14 // App headers
15 #include "pgAdmin3.h"
16
17 #include "dlg/dlgManageFavourites.h"
18 #include "db/pgConn.h"
19 #include "schema/pgServer.h"
20 #include "utils/sysLogger.h"
21
22 #include "images/folder.pngc"
23 #include "images/favourite.pngc"
24
25 #include "utils/favourites.h"
26
27 #include <wx/imaglist.h>
28
BEGIN_EVENT_TABLE(dlgManageFavourites,pgDialog)29 BEGIN_EVENT_TABLE(dlgManageFavourites, pgDialog)
30 EVT_TREE_SEL_CHANGED(XRCID("trLocation"), dlgManageFavourites::OnTreeChange)
31 EVT_BUTTON (wxID_OK, dlgManageFavourites::OnOK)
32 EVT_BUTTON (wxID_CANCEL, dlgManageFavourites::OnCancel)
33 EVT_BUTTON (XRCID("btnRename"), dlgManageFavourites::OnRename)
34 EVT_BUTTON (XRCID("btnDelete"), dlgManageFavourites::OnDelete)
35 EVT_BUTTON (XRCID("btnNewFolder"), dlgManageFavourites::OnNewFolder)
36 END_EVENT_TABLE()
37
38
39 #define btnRename CTRL_BUTTON("btnRename")
40 #define btnDelete CTRL_BUTTON("btnDelete")
41 #define btnNewFolder CTRL_BUTTON("btnNewFolder")
42 // #define txtTitle CTRL_TEXT("txtTitle")
43 #define trLocation CTRL_TREE("trLocation")
44
45
46 dlgManageFavourites::dlgManageFavourites(wxWindow *parent, queryFavouriteFolder *favourites) :
47 pgDialog()
48 {
49 SetFont(settings->GetSystemFont());
50 LoadResource(parent, wxT("dlgManageFavourites"));
51 RestorePosition();
52
53 anythingChanged = false;
54
55 this->favourites = favourites;
56
57 wxImageList *imgList = new wxImageList(16, 16, true, 2);
58 imgList->Add(*favourite_png_ico);
59 imgList->Add(*folder_png_ico);
60
61 trLocation->AssignImageList(imgList);
62
63 // Setup the default values
64 trLocation->AddRoot(_("Favourites"), 1);
65 trLocation->SetItemImage(trLocation->GetRootItem(), 1, wxTreeItemIcon_Normal);
66 trLocation->SelectItem(trLocation->GetRootItem());
67 favourites->AppendAllToTree(trLocation, trLocation->GetRootItem(), false);
68 trLocation->Expand(trLocation->GetRootItem());
69 }
70
ManageFavourites()71 int dlgManageFavourites::ManageFavourites()
72 {
73 int r = ShowModal();
74 if (r == wxID_OK)
75 return 1;
76 else
77 {
78 if (anythingChanged)
79 // Need rollback!
80 return -1;
81 else
82 return 0;
83 }
84 }
85
~dlgManageFavourites()86 dlgManageFavourites::~dlgManageFavourites()
87 {
88 SavePosition();
89 }
90
91
OnOK(wxCommandEvent & ev)92 void dlgManageFavourites::OnOK(wxCommandEvent &ev)
93 {
94 EndModal(wxID_OK);
95 }
96
97
OnCancel(wxCommandEvent & ev)98 void dlgManageFavourites::OnCancel(wxCommandEvent &ev)
99 {
100 EndModal(wxID_CANCEL);
101 }
102
OnTreeChange(wxTreeEvent & ev)103 void dlgManageFavourites::OnTreeChange(wxTreeEvent &ev)
104 {
105 if (!trLocation->GetSelection().IsOk())
106 {
107 btnRename->Enable(false);
108 btnDelete->Enable(false);
109 btnNewFolder->Enable(false);
110 return;
111 }
112 if (trLocation->GetSelection() == trLocation->GetRootItem())
113 {
114 // On root item
115 btnRename->Enable(false);
116 btnDelete->Enable(false);
117 btnNewFolder->Enable(true);
118 }
119 else
120 {
121 btnRename->Enable(true);
122 btnDelete->Enable(true);
123 btnNewFolder->Enable(trLocation->GetItemImage(trLocation->GetSelection()) == 1);
124 }
125 }
126
127
OnRename(wxCommandEvent & ev)128 void dlgManageFavourites::OnRename(wxCommandEvent &ev)
129 {
130 if (!trLocation->GetSelection().IsOk() ||
131 trLocation->GetSelection() == trLocation->GetRootItem())
132 return;
133
134 queryFavouriteItem *item = favourites->FindTreeItem(trLocation->GetSelection());
135 if (!item)
136 return;
137
138 wxTextEntryDialog dlg(this, _("Enter new name"), (item->GetId() != -2) ? _("Rename favourite") : _("Rename favourites folder"), item->GetTitle());
139 if (dlg.ShowModal() != wxID_OK)
140 return;
141
142 if (dlg.GetValue() != item->GetTitle())
143 {
144 item->SetTitle(dlg.GetValue());
145 trLocation->SetItemText(trLocation->GetSelection(), dlg.GetValue());
146 anythingChanged = true;
147 }
148 }
149
OnDelete(wxCommandEvent & ev)150 void dlgManageFavourites::OnDelete(wxCommandEvent &ev)
151 {
152 wxString msg;
153
154 if (!trLocation->GetSelection().IsOk() ||
155 trLocation->GetSelection() == trLocation->GetRootItem())
156 return;
157
158 queryFavouriteItem *item = favourites->FindTreeItem(trLocation->GetSelection());
159 if (!item)
160 return;
161
162 if (item->GetId() != -2)
163 msg = wxString::Format(_("Are you sure you want to delete the favourite '%s'?"), item->GetTitle().c_str());
164 else
165 msg = wxString::Format(_("Are you sure you want to delete the folder '%s'?"), item->GetTitle().c_str());
166 if (wxMessageDialog(this, msg, _("Confirm delete"), wxYES_NO | wxICON_QUESTION).ShowModal() != wxID_YES)
167 return;
168
169 if (favourites->DeleteTreeItem(trLocation->GetSelection()))
170 {
171 trLocation->Delete(trLocation->GetSelection());
172 anythingChanged = true;
173 }
174 }
175
OnNewFolder(wxCommandEvent & ev)176 void dlgManageFavourites::OnNewFolder(wxCommandEvent &ev)
177 {
178 if (!trLocation->GetSelection().IsOk())
179 return;
180
181 queryFavouriteItem *item = favourites->FindTreeItem(trLocation->GetSelection());
182 if (!item)
183 return;
184 if (item->GetId() != -2)
185 return;
186
187 wxTextEntryDialog dlg(this, _("Enter name of new folder"), _("Create new favourites folder"));
188 if (dlg.ShowModal() != wxID_OK)
189 return;
190
191 wxString title = dlg.GetValue().Trim();
192 if (title.IsEmpty())
193 return;
194
195 queryFavouriteFolder *fld = (queryFavouriteFolder *)item;
196 if (fld->ContainsFolder(title))
197 {
198 wxMessageBox(_("A folder with the specified name already exists."));
199 return;
200 }
201
202
203 queryFavouriteFolder *newfld = fld->AddNewFolder(dlg.GetValue());
204 newfld->SetTreeId(trLocation->AppendItem(trLocation->GetSelection(), title, 1));
205 trLocation->Expand(fld->GetTreeId());
206 anythingChanged = true;
207 }
208