Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Fixed crash in frmContactEditorOrganisations in Edit mode.
[xestiaab/.git] / source / contacteditor / frmContactEditorPicture.cpp
1 // frmContactEditorPicture.cpp - frmContactEditorPicture form.
2 //
3 // (c) 2012-2015 Xestia Software Development.
4 //
5 // This file is part of Xestia Address Book.
6 //
7 // Xestia Address Book is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by the
9 // Free Software Foundation, version 3 of the license.
10 //
11 // Xestia Address Book is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with Xestia Address Book. If not, see <http://www.gnu.org/licenses/>
19 #include "frmContactEditorPicture.h"
21 #include <wx/wx.h>
22 #include <wx/tokenzr.h>
23 #include <wx/icon.h>
24 #include <wx/mstream.h>
25 #include <wx/fs_mem.h>
26 #include <SFML/Audio.hpp>
28 #include "../common/textprocessing.h"
29 #include "../common/base64.h"
30 #include "../common/mime.h"
32 frmContactEditorPicture::frmContactEditorPicture( wxWindow* parent )
33 :
34 frmContactEditorPictureADT( parent )
35 {
36         EditorMode = FALSE;
37         sliPriority->Disable();
38         
39         cmbType->Append(wxT(""));
40         cmbType->Append(_("Home"));
41         cmbType->Append(_("Work"));
42 }
44 void frmContactEditorPicture::EnablePriority( wxCommandEvent& event )
45 {
46         if (chkUsePref->IsChecked()){
47                 sliPriority->Enable();
48         } else {
49                 sliPriority->Disable();
50         }
51 }
53 void frmContactEditorPicture::ProcessData( wxCommandEvent& event )
54 {
55         long ListCtrlIndex;
56         
57         if (EditorMode == FALSE){
59                 // Load the file into memory.
60                 
61                 wxFile PictureFile;
62                 wxString wxSPictureFilename;
63                 wxString wxSPictureMIMEType;
64                 size_t len;
65                 std::string b64enc;             
66                 
67                 wxSPictureFilename = fipPicture->GetPath();
69 /*              
70 #if wxABI_VERSION < 20900
71                 if (AudioFile.Open(wxSAudioFilename.c_str(), wxT("rb"))){
72 #else
73                 if (AudioFile.Open(wxSAudioFilename, wxT("rb"))){
74 #endif
76 */
77                 if (PictureFile.Open(wxSPictureFilename, wxFile::read)){
78                         size_t flen;
79                         len = PictureFile.Length();
80                         
81                         unsigned char* PictureFileData = new unsigned char[len];
82                         flen = PictureFile.Read(PictureFileData, len);                  
83                         
84                         // Check to make sure it is an audio file.
85                         
86                         wxMemoryInputStream istream(PictureFileData, flen);
87                         wxImage photo;                  
88                         
89                         //wxBitmap (bigimgs_contactpersonicon48i, -1);
90                         
91                         //Get MIME data.
92                         
93                         /*const char *MIMEType;
94                         magic_t MagicCookie = magic_open(MAGIC_MIME);                   
95                         magic_setflags(MagicCookie, MAGIC_MIME_TYPE);
96                         
97                         if (!MagicCookie){
99                                 wxMessageBox(_("An error occured with the support library for getting the picture type."), _("Error opening picture file"), wxICON_ERROR);                      
100                                 return;
101                                 
102                         }
103                         
104                         if (magic_load(MagicCookie, NULL) != 0) {
105                         
106                                 wxMessageBox(_("An error occured whilst determining the picture type."), _("Error opening picture file"), wxICON_ERROR);
107                                 return;
108                         
109                         }
110                         
111                         MIMEType = magic_file(MagicCookie, wxSPictureFilename.mb_str());
112                         wxSPictureMIMEType = wxString::FromUTF8(MIMEType);
113                         magic_close(MagicCookie);*/
114                         
115                         wxSPictureMIMEType = GetMIME(wxSPictureFilename);
116                         
117                         if (!photo.LoadFile(istream, wxBITMAP_TYPE_ANY)){
119                                 wxMessageBox(_("This file is in an picture format that is unsupported or is not a picture file."), _("Error opening picture file"), wxICON_ERROR);
120                                 return;                 
121                                                         
122                         
123                         }
124                         
125                         // Covert the file into base64.
126                 
127                         b64enc = base64_encode(PictureFileData, (int)len);                      
128                         
129                         delete[] PictureFileData;
130         
131                 } else {
132         
133                         wxMessageBox(_("There was an error opening the audio file!"), _("Error opening audio file"), wxICON_ERROR);
134         
135                         return;
136         
137                 }               
139                 PictureListPtr->insert(std::make_pair(PictureListIndex, b64enc));
141                 wxString strValue;              
142                 strValue.Clear();
143                 
144                 strValue = cmbType->GetString(cmbType->GetSelection());
146                 // Setup Picture Type.
148                 if (strValue == _("Home")) {
149                 
150                         PictureListTypePtr->insert(std::make_pair(PictureListIndex, wxT("home")));
151                 
152                 } else if (strValue == _("Work")) {
153                 
154                         PictureListTypePtr->insert(std::make_pair(PictureListIndex, wxT("work")));
155                 
156                 } else {
157                 
158                         PictureListTypePtr->insert(std::make_pair(PictureListIndex, wxT("")));
159                 
160                 }
161                 
162                 // Setup Organisation Priority.
163                 
164                 if (chkUsePref->IsChecked()){
165                 
166                         PictureListPrefPtr->insert(std::make_pair(PictureListIndex, sliPriority->GetValue()));
167                 
168                 } else {
169                 
170                         PictureListPrefPtr->insert(std::make_pair(PictureListIndex, 0));
171                 
172                 }
173                 
174                 // Setup MIME Type.
175                 
176                 PictureListPictureTypePtr->insert(std::make_pair(PictureListIndex, wxSPictureMIMEType));
177                 
178                 // Setup encoding type.
179                 
180                 PictureListPicEncTypePtr->insert(std::make_pair(PictureListIndex, wxT("base64")));
181                 
182                 // Add to form.
183                 
184                 wxListItem coldata;
185                 
186                 coldata.SetId(PictureListIndex);
187                 coldata.SetData(PictureListIndex);
188                 
189                 if (PictureLogo == TRUE){
190                 
191                         coldata.SetText(_("Picture"));
192                 
193                 } else {
195                         coldata.SetText(_("Logo"));
196                 
197                 }
198                 
199                 ListCtrlIndex = PictureListCtrlPtr->InsertItem(coldata);
200                 
201                 if (strValue == _("Home")) {
202                 
203                         PictureListCtrlPtr->SetItem(ListCtrlIndex, 1, _("Home"));
204                 
205                 } else if (strValue == _("Work")) {
206                 
207                         PictureListCtrlPtr->SetItem(ListCtrlIndex, 1, _("Work"));
208                 
209                 } else {
210                 
212                 
213                 }               
214                 
215                 if (chkUsePref->IsChecked()){
216                 
217                         PictureListCtrlPtr->SetItem(ListCtrlIndex, 2, wxString::Format(wxT("%i"), sliPriority->GetValue()));
218                 
219                 }
220                 
221                 this->Close();
222                 
223         } else if (EditorMode == TRUE) {
224         
225                 // Do not touch the audio file but update the settings
226                 // associated with the audio file.
227         
228                 long longSelected = -1;
229                 wxString strValue;      
230                 
231                 // Update Picture Type.
232                 
233                 PictureListTypePtr->erase(PictureListIndex);
234                 strValue = cmbType->GetString(cmbType->GetSelection());         
235                 
236                 if (strValue == _("Home")) {
237                 
238                         PictureListTypePtr->insert(std::make_pair(PictureListIndex, wxT("home")));
239                 
240                 } else if (strValue == _("Work")) {
241                 
242                         PictureListTypePtr->insert(std::make_pair(PictureListIndex, wxT("work")));
243                 
244                 }               
245                 
246                 // Update Category Priority.
247                 
248                 PictureListPrefPtr->erase(PictureListIndex);
249                 
250                 if (chkUsePref->IsChecked()){
251                 
252                         PictureListPrefPtr->insert(std::make_pair(PictureListIndex, sliPriority->GetValue()));
253                 
254                 } else {
255                 
256                         PictureListPrefPtr->insert(std::make_pair(PictureListIndex, 0));
257                 
258                 }
259                 
260                 // Update form.
261                 
262                 longSelected = PictureListCtrlPtr->GetNextItem(longSelected, 
263                         wxLIST_NEXT_ALL,
264                         wxLIST_STATE_SELECTED);
265                         
266                 if (longSelected == -1){
267                         return;
268                 }               
269                 
270                 if (PictureLogo == TRUE){
271                 
272                         PictureListCtrlPtr->SetItem(longSelected, 0, _("Picture"));
273                         
274                 } else {
275                 
276                         PictureListCtrlPtr->SetItem(longSelected, 0, _("Logo"));
277                 
278                 }
279                 
280                 PictureListCtrlPtr->SetItem(longSelected, 1, strValue);
281                 
282                 if (chkUsePref->IsChecked()){
283                 
284                         PictureListCtrlPtr->SetItem(longSelected, 2, wxString::Format(wxT("%i"), sliPriority->GetValue()));
285                 
286                 } else {
287                 
288                         PictureListCtrlPtr->SetItem(longSelected, 2, wxT(""));
289                 
290                 }
291                 
292                 this->Close();  
293         
294         }
297 void frmContactEditorPicture::CloseWindow( wxCommandEvent& event )
299         this->Close();
302 void frmContactEditorPicture::SetEditorMode(bool EditMode)
304         // Set if the editor is adding or editing an address.
305         // FALSE = Add
306         // TRUE = Edit
307         
308         if (EditMode == FALSE){
309         
310                 EditorMode = FALSE;
311                 btnAction->SetLabel(_("Add"));
312                 this->SetTitle(_("Add Picture"));
313         
314         } else if (EditMode == TRUE){
315         
316                 EditorMode = TRUE;
317                 btnAction->SetLabel(_("Modify"));
318                 this->SetTitle(_("Modify Picture"));
319                 fipPicture->Disable();
320                 
321                 std::map<int,int>::iterator intiter;
322                 std::map<int,wxString>::iterator striter;               
323                 wxString strValue;
324                 
325                 // Load the data into the form. Get the website.
326                 
327                 striter = PictureListTypePtr->find(PictureListIndex);
328                  
329                 if (striter->first == PictureListIndex){
330                 
331                         strValue = striter->second;
332                 
333                 }
334                 
335                 if (strValue == wxT("home")){
336                 
337                         cmbType->SetSelection(1);
338                 
339                 } else if (strValue == wxT("work")){ 
340                 
341                         cmbType->SetSelection(2);
342                 
343                 } else {
344                 
345                         cmbType->SetSelection(0);
346                 
347                 }
348                 
349                 //strValue.Clear();
350                 
351                 // Get the website priority.
352                 
353                 intiter = PictureListPrefPtr->find(PictureListIndex);
354                 
355                 if (intiter->first == PictureListIndex && intiter->second > 0){
356                 
357                         sliPriority->SetValue(intiter->second);
358                         sliPriority->Enable();
359                         chkUsePref->SetValue(TRUE);
360                 
361                 }       
362                 
363         }
364                 
367 void frmContactEditorPicture::SetupPointers(std::map<int, std::string> *PictureList,                    
368         std::map<int, wxString> *PictureListAltID,
369         std::map<int, wxString> *PictureListPID,
370         std::map<int, wxString> *PictureListType,
371         std::map<int, wxString> *PictureListPicEncType, 
372         std::map<int, wxString> *PictureListPictureType,        
373         std::map<int, wxString> *PictureListTokens,
374         std::map<int, wxString> *PictureListMediatype,  
375         std::map<int, int> *PictureListPref,
376         wxListCtrl *PictureListCtrl,
377         int PictureIndex,
378         bool PictureLogoIn )
381         PictureListPtr = PictureList;
382         PictureListAltIDPtr = PictureListAltID;
383         PictureListPIDPtr = PictureListPID;
384         PictureListPicEncTypePtr = PictureListPicEncType;       
385         PictureListPictureTypePtr = PictureListPictureType;
386         PictureListTypePtr = PictureListType;
387         PictureListTokensPtr = PictureListTokens;
388         PictureListMediatypePtr = PictureListMediatype; 
389         PictureListPrefPtr = PictureListPref;
390         PictureListCtrlPtr = PictureListCtrl;
391         PictureListIndex = PictureIndex;
392         PictureLogo = PictureLogoIn;
393         
Xestia Software Development
Yn Maystri
© 2006 - 2019 Xestia Software Development
Software

Xestia Address Book
Xestia Calendar
Development

Xestia Gelforn
Everything else

About
News
Privacy Policy