Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
frmContactEditor: Stop XAB crashing when pressing Modify/Delete
[xestiaab/.git] / source / contacteditor / frmContactEditor-Picture.cpp
index 5412b99..fd9d169 100644 (file)
+// frmContactEditor-Picture.cpp - frmContactEditor Picture tab subroutines.
+//
+// (c) 2012-2016 Xestia Software Development.
+//
+// This file is part of Xestia Address Book.
+//
+// Xestia Address Book is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by the
+// Free Software Foundation, version 3 of the license.
+//
+// Xestia Address Book is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with Xestia Address Book. If not, see <http://www.gnu.org/licenses/>
+
+#include <wx/file.h>
 #include "frmContactEditor.h"
 #include "frmContactEditorPicture.h"
 #include "../common/base64.h"
 
 void frmContactEditor::AddPicture( wxCommandEvent& event )
 {
-    int intResult = 0;
-    
-    frmContactEditorPicture *frameCEPicture = new frmContactEditorPicture ( this );
-    frameCEPicture->SetEditorMode(FALSE);
-    intResult = GetLastInt(&PicturesList);
-    frameCEPicture->SetupPointers(&PicturesList,
-                                  &PicturesListAltID,
-                                  &PicturesListPID,
-                                  &PicturesListType,
-                                  &PicturesListPicEncType,
-                                  &PicturesListPictureType,
-                                  &PicturesListTokens,
-                                  &PicturesListMediatype,
-                                  &PicturesListPref,
-                                  lboPictures,
-                                  (intValueSeek));
-    frameCEPicture->ShowModal();
-    delete frameCEPicture;
-    frameCEPicture = NULL;
+       
+       // Bring up window for adding a picture.
+           
+       frmContactEditorPicture *frameCEPicture = new frmContactEditorPicture ( this );
+       frameCEPicture->SetEditorMode(FALSE);
+       frameCEPicture->SetupPointers(&ContactEditorData.PicturesList,
+               &ContactEditorData.PicturesListAltID,
+               &ContactEditorData.PicturesListPID,
+                &ContactEditorData.PicturesListType,
+                &ContactEditorData.PicturesListPicEncType,
+                &ContactEditorData.PicturesListPictureType,
+                &ContactEditorData.PicturesListTokens,
+                &ContactEditorData.PicturesListMediatype,
+                &ContactEditorData.PicturesListPref,
+                lboPictures,
+                (intValueSeek),
+               TRUE);
+       frameCEPicture->ShowModal();
+       delete frameCEPicture;
+       frameCEPicture = NULL;
+       
 }
 
 void frmContactEditor::ModifyPicture( wxCommandEvent& event )
 {
-    long longSelected = -1;
-    int intSelectedData = 0;
-    
-    if (!GetSelectedItem(lboPictures,
-                         &longSelected,
-                         &intSelectedData)){
-        return;
-    }
-    
-    frmContactEditorPicture *frameCEPicture = new frmContactEditorPicture ( this );
-    frameCEPicture->SetupPointers(&PicturesList,
-                                  &PicturesListAltID,
-                                  &PicturesListPID,
-                                  &PicturesListType,
-                                  &PicturesListPicEncType,
-                                  &PicturesListPictureType,
-                                  &PicturesListTokens,
-                                  &PicturesListMediatype,
-                                  &PicturesListPref,
-                                  lboPictures,
-                                  intSelectedData);
-    frameCEPicture->SetEditorMode(TRUE);
-    frameCEPicture->ShowModal();
-    delete frameCEPicture;
-    frameCEPicture = NULL;
+       
+       // Bring up window for modifying a picture.
+       
+       long longSelected = -1;
+       int intSelectedData = 0;
+    
+       if (lboPictures->GetItemCount() == 0 || !GetSelectedItem(lboPictures,
+               &longSelected,
+                &intSelectedData)){
+               return;
+       }
+    
+       frmContactEditorPicture *frameCEPicture = new frmContactEditorPicture ( this );
+       frameCEPicture->SetupPointers(&ContactEditorData.PicturesList,
+               &ContactEditorData.PicturesListAltID,
+                &ContactEditorData.PicturesListPID,
+                &ContactEditorData.PicturesListType,
+                &ContactEditorData.PicturesListPicEncType,
+                &ContactEditorData.PicturesListPictureType,
+                &ContactEditorData.PicturesListTokens,
+                &ContactEditorData.PicturesListMediatype,
+                &ContactEditorData.PicturesListPref,
+                lboPictures,
+                intSelectedData,
+               TRUE);
+       frameCEPicture->SetEditorMode(TRUE);
+       frameCEPicture->ShowModal();
+       delete frameCEPicture;
+       frameCEPicture = NULL;
     
 }
 
 void frmContactEditor::DeletePicture( wxCommandEvent& event )
 {
-    long longSelected = -1;
-    int intSelectedData = 0;
-    
-    if (!GetSelectedItem(lboPictures,
-                         &longSelected,
-                         &intSelectedData)){
-        return;
-    }
-    
-    lboPictures->DeleteItem(longSelected);
-    
-    DeleteMapData(intSelectedData,
-                  &PicturesList, &PicturesListAltID, &PicturesListPID, &PicturesListType,
-                  &PicturesListPicEncType, &PicturesListPictureType, &PicturesListTokens,
-                  &PicturesListMediatype, &PicturesListPref);
-    
-    wxMemoryInputStream istream(misc_emptyimage_png, sizeof(misc_emptyimage_png));
-    wxImage misc_emptyimage_png(istream, wxBITMAP_TYPE_PNG);
-    PictureImage = (wxBitmap)misc_emptyimage_png;
-    
-    imgPicture->SetBitmap(PictureImage);
+       
+       // Bring up window for deleting a picture.
+       
+       long longSelected = -1;
+       int intSelectedData = 0;
+    
+       if (lboPictures->GetItemCount() == 0 || !GetSelectedItem(lboPictures,
+               &longSelected,
+                &intSelectedData)){
+               return;
+       }
+    
+       lboPictures->DeleteItem(longSelected);
+    
+       DeleteMapData(intSelectedData,
+                &ContactEditorData.PicturesList, &ContactEditorData.PicturesListAltID, &ContactEditorData.PicturesListPID, &ContactEditorData.PicturesListType,
+                &ContactEditorData.PicturesListPicEncType, &ContactEditorData.PicturesListPictureType, &ContactEditorData.PicturesListTokens,
+               &ContactEditorData.PicturesListMediatype, &ContactEditorData.PicturesListPref);
+    
+       wxMemoryInputStream istream(misc_emptyimage_png, sizeof(misc_emptyimage_png));
+       wxImage misc_emptyimage_png(istream, wxBITMAP_TYPE_PNG);
+       PictureImage = (wxBitmap)misc_emptyimage_png;
+    
+       imgPicture->SetBitmap(PictureImage);
+       
 }
 
 void frmContactEditor::LoadPicture( wxListEvent& event )
 {
     
-    long longSelected = -1;
-    int intSelectedData = 0;
-    
-    if (!GetSelectedItem(lboPictures,
-                         &longSelected,
-                         &intSelectedData)){
-        return;
-    }
+       // Load the picture.
+       
+       long longSelected = -1;
+       int intSelectedData = 0;
     
-    std::string base64dec;
+       if (lboPictures->GetItemCount() == 0 || !GetSelectedItem(lboPictures,
+               &longSelected,
+               &intSelectedData)){
+               return;
+       }
     
-    std::map<int,std::string>::iterator initier;
-    std::map<int,wxString>::iterator pictypeiter;
+       std::string base64dec;
+
+       std::map<int,std::string>::iterator initier;
+       std::map<int,wxString>::iterator pictypeiter;
     
-    initier = PicturesList.find(intSelectedData);
-    pictypeiter = PicturesListPictureType.find(intSelectedData);
+       initier = ContactEditorData.PicturesList.find(intSelectedData);
+       pictypeiter = ContactEditorData.PicturesListPictureType.find(intSelectedData);
     
-    base64dec = base64_decode(initier->second);
+       base64dec = base64_decode(initier->second);
     
-    wxMemoryInputStream istream(base64dec.c_str(), (size_t)base64dec.size());
-    wxImage photo;
-    //wxMemoryInputStream istream(base64dec, (size_t)base64dec.size());
+       wxMemoryInputStream istream(base64dec.c_str(), (size_t)base64dec.size());
+       wxImage photo;
     
-    if (pictypeiter->second == wxT("image/jpeg")){
+       if (pictypeiter->second == wxT("image/jpeg")){
         
-        if (!photo.LoadFile(istream, wxBITMAP_TYPE_JPEG)){
+               if (!photo.LoadFile(istream, wxBITMAP_TYPE_JPEG)){
         
-            return;
+                       return;
         
-        }
+               }
         
-    } else if (pictypeiter->second == wxT("image/png")){
+       } else if (pictypeiter->second == wxT("image/png")){
         
-        if (!photo.LoadFile(istream, wxBITMAP_TYPE_PNG)){
+               if (!photo.LoadFile(istream, wxBITMAP_TYPE_PNG)){
             
-            return;
+                       return;
             
-        }
+               }
         
-    } else if (pictypeiter->second == wxT("image/gif")){
+       } else if (pictypeiter->second == wxT("image/gif")){
         
-        if (!photo.LoadFile(istream, wxBITMAP_TYPE_GIF)){
+               if (!photo.LoadFile(istream, wxBITMAP_TYPE_GIF)){
             
-            return;
+                       return;
             
-        }
+               }
         
-    } else {
+       } else {
     
-        return;
+               return;
     
-    }
+       }
     
-    PictureImage = (wxBitmap)photo;
-    imgPicture->SetBitmap(PictureImage);
+       PictureImage = (wxBitmap)photo;
+       imgPicture->SetBitmap(PictureImage);
     
 }
+
+void frmContactEditor::SavePicture( wxCommandEvent &event )
+{
+
+       // Save the picture to a file.
+       
+       long longSelected = -1;
+       int intSelectedData = 0;
+    
+       if (lboPictures->GetItemCount() == 0 || !GetSelectedItem(lboPictures,
+                         &longSelected,
+                         &intSelectedData)){
+               
+               return;
+               
+       }
+       
+       wxString FileTypes;
+       
+       wxString FinalFilename;
+       
+       std::map<int,std::string>::iterator initier;
+       std::map<int,wxString>::iterator pictypeiter;
+    
+       initier = ContactEditorData.PicturesList.find(intSelectedData);
+       pictypeiter = ContactEditorData.PicturesListPictureType.find(intSelectedData);
+       
+       if (pictypeiter->second == wxT("image/jpeg")){
+    
+               FileTypes.Append("JPEG Image (*.jpeg)|*.jpeg");
+       
+       } else if (pictypeiter->second == wxT("image/png")){
+
+               FileTypes.Append("PNG Image (*.png)|*.png");        
+        
+       } else if (pictypeiter->second == wxT("image/gif")){
+        
+               FileTypes.Append("GIF Image (*.gif)|*.gif"); 
+       
+       } else {
+
+               wxMessageBox(_("Picture format for saving is not supported."), _("Picture format unsupported"), wxOK, this);
+               return;
+
+       }
+       
+       // Open up the dialog to save the picture.
+       
+       wxFileDialog ExportDlg(this, wxT("Save Picture"), wxT(""), wxT(""),
+               FileTypes, wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
+       
+       if (ExportDlg.ShowModal() == wxID_CANCEL){
+       
+               return;
+       
+       }
+       
+       // Write out the picture to the file.
+       
+       wxFile PictureFile;
+       
+       if (PictureFile.Open(ExportDlg.GetPath(), wxFile::write)){
+       
+               std::string base64dec;
+               
+               initier = ContactEditorData.PicturesList.find(intSelectedData);
+               base64dec = base64_decode(initier->second);
+               
+               PictureFile.Write(base64dec.c_str(), (size_t)base64dec.size());
+
+               PictureFile.Close();
+       
+       } else {
+       
+               wxMessageBox(_("An error occured whilst saving the picture. Check that you have free space and permissions to write the picture at this location."), _("Picture save error"), wxOK, this);
+               return;
+       
+       }
+                       
+}
\ No newline at end of file
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