1 /*******************************************************
2 Copyright (C) 2014 Gabriele-V
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 ********************************************************/
18
19 #include "webappdialog.h"
20 #include "constants.h"
21 #include "paths.h"
22 #include "transdialog.h"
23 #include "util.h"
24 #include "webapp.h"
25
26 #include "../resources/magic_wand.xpm"
27
28 wxIMPLEMENT_DYNAMIC_CLASS(mmWebAppDialog, wxDialog);
29
wxBEGIN_EVENT_TABLE(mmWebAppDialog,wxDialog)30 wxBEGIN_EVENT_TABLE(mmWebAppDialog, wxDialog)
31 EVT_BUTTON(wxID_CANCEL, mmWebAppDialog::OnCancel)
32 EVT_BUTTON(wxID_APPLY, mmWebAppDialog::OnApply)
33 EVT_BUTTON(wxID_OK, mmWebAppDialog::OnOk)
34 EVT_DATAVIEW_ITEM_ACTIVATED(wxID_ANY, mmWebAppDialog::OnListItemActivated)
35 EVT_DATAVIEW_ITEM_CONTEXT_MENU(wxID_ANY, mmWebAppDialog::OnItemRightClick)
36 EVT_MENU_RANGE(MENU_OPEN_ATTACHMENT, MENU_DELETE_WEBTRAN, mmWebAppDialog::OnMenuSelected)
37 wxEND_EVENT_TABLE()
38
39
40 mmWebAppDialog::mmWebAppDialog(wxWindow *parent) :
41 m_webtran_id(-1)
42 , m_maskTextCtrl()
43 , webtranListBox_()
44 , refreshRequested_(false)
45 {
46 do_create(parent);
47 }
48
do_create(wxWindow * parent)49 void mmWebAppDialog::do_create(wxWindow* parent)
50 {
51 SetExtraStyle(GetExtraStyle() | wxWS_EX_BLOCK_EVENTS);
52
53 long style = wxCAPTION | wxCLOSE_BOX | wxRESIZE_BORDER;
54 if (!wxDialog::Create(parent, wxID_ANY, _("Import WebApp transactions")
55 , wxDefaultPosition, wxDefaultSize, style))
56 {
57 return;
58 }
59
60 CreateControls();
61
62 GetSizer()->Fit(this);
63 GetSizer()->SetSizeHints(this);
64
65 SetIcon(mmex::getProgramIcon());
66
67 fillControls();
68
69 Centre();
70 }
71
CreateControls()72 void mmWebAppDialog::CreateControls()
73 {
74 wxBoxSizer* mainBoxSizer = new wxBoxSizer(wxVERTICAL);
75
76 webtranListBox_ = new wxDataViewListCtrl(this, wxID_ANY, wxDefaultPosition, wxSize(800, 500), wxDV_MULTIPLE | wxDV_ROW_LINES);
77
78 webtranListBox_->AppendTextColumn("#", wxDATAVIEW_CELL_INERT, wxLIST_AUTOSIZE_USEHEADER); //WEBTRAN_ID
79 webtranListBox_->AppendTextColumn(_("Date"), wxDATAVIEW_CELL_INERT, wxLIST_AUTOSIZE_USEHEADER); //WEBTRAN_DATE
80 webtranListBox_->AppendTextColumn(_("Account"), wxDATAVIEW_CELL_INERT, wxLIST_AUTOSIZE_USEHEADER); //WEBTRAN_ACCOUNT
81 webtranListBox_->AppendTextColumn(_("Status"), wxDATAVIEW_CELL_INERT, wxLIST_AUTOSIZE_USEHEADER); //WEBTRAN_STATUS
82 webtranListBox_->AppendTextColumn(_("Type"), wxDATAVIEW_CELL_INERT, wxLIST_AUTOSIZE_USEHEADER); //WEBTRAN_TYPE
83 webtranListBox_->AppendTextColumn(_("Payee"), wxDATAVIEW_CELL_INERT, wxLIST_AUTOSIZE_USEHEADER); //WEBTRAN_PAYEE,
84 webtranListBox_->AppendTextColumn(_("Category"), wxDATAVIEW_CELL_INERT, wxLIST_AUTOSIZE_USEHEADER); //WEBTRAN_CATEGORY,
85 webtranListBox_->AppendTextColumn(_("Amount"), wxDATAVIEW_CELL_INERT, wxLIST_AUTOSIZE_USEHEADER); //WEBTRAN_AMOUNT,
86 webtranListBox_->AppendTextColumn(_("Notes"), wxDATAVIEW_CELL_INERT, wxLIST_AUTOSIZE_USEHEADER); //WEBTRAN_NOTES,
87 webtranListBox_->AppendTextColumn(_("Attachments"), wxDATAVIEW_CELL_INERT, wxLIST_AUTOSIZE_USEHEADER); //WEBTRAN_ATTACHMENTS,
88 mainBoxSizer->Add(webtranListBox_, wxSizerFlags(g_flagsExpand).Border(wxALL, 10));
89
90 wxPanel* buttons_panel = new wxPanel(this, wxID_ANY);
91 mainBoxSizer->Add(buttons_panel, wxSizerFlags(g_flagsExpand).Proportion(0));
92 wxBoxSizer* tools_sizer = new wxBoxSizer(wxVERTICAL);
93 buttons_panel->SetSizer(tools_sizer);
94
95 wxStdDialogButtonSizer* buttons_sizer = new wxStdDialogButtonSizer;
96 tools_sizer->Add(buttons_sizer, wxSizerFlags(g_flags).Center());
97 wxButton* buttonOK = new wxButton(buttons_panel, wxID_OK, _("&Import all "));
98 wxButton* buttonApply = new wxButton(buttons_panel, wxID_APPLY, _("Import and open all "));
99 wxButton* btnCancel = new wxButton(buttons_panel, wxID_CANCEL, wxGetTranslation(g_CancelLabel));
100
101 buttons_sizer->Add(buttonOK, g_flags);
102 buttons_sizer->Add(buttonApply, g_flags);
103 buttons_sizer->Add(btnCancel, g_flags);
104
105 this->SetSizer(mainBoxSizer);
106 }
107
fillControls()108 void mmWebAppDialog::fillControls()
109 {
110 webtranListBox_->DeleteAllItems();
111 WebAppTransactions_.clear();
112
113 mmWebApp::WebApp_DownloadNewTransaction(WebAppTransactions_, false);
114
115 for (const auto& WebTran : WebAppTransactions_)
116 {
117 wxVector<wxVariant> data;
118 data.push_back(wxVariant(wxString::Format(wxT("%i"),WebTran.ID))); //WEBTRAN_ID
119 data.push_back(wxVariant(mmGetDateForDisplay(WebTran.Date))); //WEBTRAN_DATE
120 data.push_back(wxVariant(WebTran.Account)); //WEBTRAN_ACCOUNT
121 data.push_back(wxVariant(WebTran.Status)); //WEBTRAN_STATUS
122 data.push_back(wxVariant(WebTran.Type)); //WEBTRAN_TYPE
123
124 wxString Payee = WebTran.Type != "Transfer" ? WebTran.Payee : "> " + WebTran.ToAccount;
125 data.push_back(wxVariant(Payee)); //WEBTRAN_PAYEE
126
127 wxString Category = WebTran.Category;
128 if (WebTran.SubCategory != wxEmptyString) Category += ":" + WebTran.SubCategory;
129 data.push_back(wxVariant(Category)); //WEBTRAN_CATEGORY
130
131 data.push_back(wxVariant(wxString::Format(wxT("%f"),WebTran.Amount)));
132 data.push_back(wxVariant(WebTran.Notes)); //WEBTRAN_NOTES
133 data.push_back(wxVariant(WebTran.Attachments)); //WEBTRAN_ATTACHMENTS
134 webtranListBox_->AppendItem(data, (wxUIntPtr)WebTran.ID);
135 }
136 }
137
OnListItemActivated(wxDataViewEvent & event)138 void mmWebAppDialog::OnListItemActivated(wxDataViewEvent& event)
139 {
140 wxDataViewItem item = event.GetItem();
141 int selected_index = webtranListBox_->ItemToRow(item);
142
143 if (selected_index >= 0)
144 {
145 int WebTrID = (int)webtranListBox_->GetItemData(item);
146 mmWebAppDialog::ImportWebTr(WebTrID, true);
147 fillControls();
148 }
149 }
150
ImportWebTr(int WebTrID,bool open)151 bool mmWebAppDialog::ImportWebTr(int WebTrID, bool open)
152 {
153 mmWebApp::webtran_holder WebTrToImport;
154 bool bFound = false;
155
156 for (const auto WebTr : WebAppTransactions_)
157 {
158 if (WebTr.ID == WebTrID)
159 {
160 bFound = true;
161 WebTrToImport = WebTr;
162 int InsertedTransactionID = mmWebApp::MMEX_InsertNewTransaction(WebTrToImport);
163 if (InsertedTransactionID > 0)
164 {
165 if (open)
166 {
167 //fillControls(); //TODO: Delete transaction from view
168 mmTransDialog EditTransactionDialog(this, 1, InsertedTransactionID);
169 EditTransactionDialog.ShowModal();
170 }
171 refreshRequested_ = true;
172 }
173 else
174 {
175 bFound = false;
176 }
177 break;
178 }
179 }
180 if (!bFound)
181 {
182 wxString msgStr = wxString() << _("Unable to insert transaction in MMEX database") << "\n";
183 wxMessageBox(msgStr, _("WebApp communication error"), wxICON_ERROR);
184 }
185
186 return bFound;
187 }
188
OpenAttachment()189 void mmWebAppDialog::OpenAttachment()
190 {
191 wxDataViewItemArray Selected;
192 webtranListBox_->GetSelections(Selected);
193
194 for (wxDataViewItem Item : Selected)
195 {
196 int selectedIndex_ = webtranListBox_->ItemToRow(Item);
197 if (selectedIndex_ >= 0)
198 {
199 wxString AttachmentName = webtranListBox_->GetTextValue(selectedIndex_, WEBTRAN_ATTACHMENTS);
200 AttachmentName = AttachmentName.BeforeFirst(';');
201 wxString Path = mmWebApp::WebApp_GetAttachment(AttachmentName);
202 if (Path != wxEmptyString)
203 wxLaunchDefaultApplication(Path);
204 }
205 }
206 }
207
ImportWebTrSelected()208 void mmWebAppDialog::ImportWebTrSelected()
209 {
210 wxDataViewItemArray Selected;
211 webtranListBox_->GetSelections(Selected);
212
213 if (Selected.size() == 0)
214 return;
215
216 for (wxDataViewItem Item : Selected)
217 {
218 int selectedIndex_ = webtranListBox_->ItemToRow(Item);
219 if (selectedIndex_ >= 0)
220 {
221 int WebTrID = (int)webtranListBox_->GetItemData(Item);
222 mmWebAppDialog::ImportWebTr(WebTrID, true);
223 }
224 }
225 fillControls();
226 }
227
DeleteWebTr()228 void mmWebAppDialog::DeleteWebTr()
229 {
230 wxDataViewItemArray Selected;
231 webtranListBox_->GetSelections(Selected);
232
233 if (Selected.size() == 0)
234 return;
235
236 for (wxDataViewItem Item : Selected)
237 {
238 int selectedIndex_ = webtranListBox_->ItemToRow(Item);
239 if (selectedIndex_ >= 0)
240 {
241 mmWebApp::WebApp_DeleteOneTransaction((int)webtranListBox_->GetItemData(Item));
242 }
243 }
244 fillControls();
245 }
246
OnMenuSelected(wxCommandEvent & event)247 void mmWebAppDialog::OnMenuSelected(wxCommandEvent& event)
248 {
249 wxCommandEvent evt;
250
251 switch (event.GetId())
252 {
253 case MENU_OPEN_ATTACHMENT: OpenAttachment(); break;
254 case MENU_IMPORT_WEBTRAN: ImportWebTrSelected(); break;
255 case MENU_DELETE_WEBTRAN: DeleteWebTr(); break;
256 default: break;
257 }
258 }
259
OnItemRightClick(wxDataViewEvent & event)260 void mmWebAppDialog::OnItemRightClick(wxDataViewEvent& event)
261 {
262 wxDataViewItemArray Selected;
263 webtranListBox_->GetSelections(Selected);
264
265 wxCommandEvent evt(wxEVT_COMMAND_MENU_SELECTED, wxID_ANY);
266 evt.SetEventObject(this);
267
268 wxMenu* mainMenu = new wxMenu;
269 mainMenu->Append(new wxMenuItem(mainMenu, MENU_OPEN_ATTACHMENT, _("Open Attachment")));
270 mainMenu->Append(new wxMenuItem(mainMenu, MENU_IMPORT_WEBTRAN, _("Import")));
271 mainMenu->Append(new wxMenuItem(mainMenu, MENU_DELETE_WEBTRAN, _("Delete")));
272 if (Selected.size() != 1) mainMenu->Enable(MENU_OPEN_ATTACHMENT, false);
273
274 PopupMenu(mainMenu);
275 delete mainMenu;
276 event.Skip();
277 }
278
279
ImportAllWebTr(const bool & open)280 void mmWebAppDialog::ImportAllWebTr(const bool& open)
281 {
282 for (int i = 0; i < webtranListBox_->GetItemCount(); i++)
283 {
284 int WebTrID = wxAtoi(webtranListBox_->GetTextValue(i, WEBTRAN_ID));
285 mmWebAppDialog::ImportWebTr(WebTrID, open);
286 }
287 }
288
OnCancel(wxCommandEvent &)289 void mmWebAppDialog::OnCancel(wxCommandEvent& /*event*/)
290 {
291 EndModal(wxID_CANCEL);
292 }
293
OnApply(wxCommandEvent &)294 void mmWebAppDialog::OnApply(wxCommandEvent& /*event*/)
295 {
296 mmWebAppDialog::ImportAllWebTr(true);
297 EndModal(wxID_APPLY);
298 }
299
OnOk(wxCommandEvent &)300 void mmWebAppDialog::OnOk(wxCommandEvent& /*event*/)
301 {
302 mmWebAppDialog::ImportAllWebTr(false);
303 EndModal(wxID_OK);
304 }
305