// frmContactEditorPicture.cpp - frmContactEditorPicture form.
//
// (c) 2012-2015 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
#include "frmContactEditorPicture.h"
#include
#include
#include
#include
#include
#include
#include "../common/textprocessing.h"
#include "../common/base64.h"
#include "../common/mime.h"
frmContactEditorPicture::frmContactEditorPicture( wxWindow* parent )
:
frmContactEditorPictureADT( parent )
{
EditorMode = FALSE;
sliPriority->Disable();
cmbType->Append(wxT(""));
cmbType->Append(_("Home"));
cmbType->Append(_("Work"));
}
void frmContactEditorPicture::EnablePriority( wxCommandEvent& event )
{
if (chkUsePref->IsChecked()){
sliPriority->Enable();
} else {
sliPriority->Disable();
}
}
void frmContactEditorPicture::ProcessData( wxCommandEvent& event )
{
long ListCtrlIndex;
if (EditorMode == FALSE){
// Load the file into memory.
wxFile PictureFile;
wxString wxSPictureFilename;
wxString wxSPictureMIMEType;
size_t len;
std::string b64enc;
wxSPictureFilename = fipPicture->GetPath();
/*
#if wxABI_VERSION < 20900
if (AudioFile.Open(wxSAudioFilename.c_str(), wxT("rb"))){
#else
if (AudioFile.Open(wxSAudioFilename, wxT("rb"))){
#endif
*/
if (PictureFile.Open(wxSPictureFilename, wxFile::read)){
size_t flen;
len = PictureFile.Length();
unsigned char* PictureFileData = new unsigned char[len];
flen = PictureFile.Read(PictureFileData, len);
// Check to make sure it is an audio file.
wxMemoryInputStream istream(PictureFileData, flen);
wxImage photo;
//wxBitmap (bigimgs_contactpersonicon48i, -1);
//Get MIME data.
/*const char *MIMEType;
magic_t MagicCookie = magic_open(MAGIC_MIME);
magic_setflags(MagicCookie, MAGIC_MIME_TYPE);
if (!MagicCookie){
wxMessageBox(_("An error occured with the support library for getting the picture type."), _("Error opening picture file"), wxICON_ERROR);
return;
}
if (magic_load(MagicCookie, NULL) != 0) {
wxMessageBox(_("An error occured whilst determining the picture type."), _("Error opening picture file"), wxICON_ERROR);
return;
}
MIMEType = magic_file(MagicCookie, wxSPictureFilename.mb_str());
wxSPictureMIMEType = wxString::FromUTF8(MIMEType);
magic_close(MagicCookie);*/
wxSPictureMIMEType = GetMIME(wxSPictureFilename);
if (!photo.LoadFile(istream, wxBITMAP_TYPE_ANY)){
wxMessageBox(_("This file is in an picture format that is unsupported or is not a picture file."), _("Error opening picture file"), wxICON_ERROR);
return;
}
// Covert the file into base64.
b64enc = base64_encode(PictureFileData, (int)len);
delete[] PictureFileData;
} else {
wxMessageBox(_("There was an error opening the audio file!"), _("Error opening audio file"), wxICON_ERROR);
return;
}
PictureListPtr->insert(std::make_pair(PictureListIndex, b64enc));
wxString strValue;
strValue.Clear();
strValue = cmbType->GetString(cmbType->GetSelection());
// Setup Picture Type.
if (strValue == _("Home")) {
PictureListTypePtr->insert(std::make_pair(PictureListIndex, wxT("home")));
} else if (strValue == _("Work")) {
PictureListTypePtr->insert(std::make_pair(PictureListIndex, wxT("work")));
} else {
PictureListTypePtr->insert(std::make_pair(PictureListIndex, wxT("")));
}
// Setup Organisation Priority.
if (chkUsePref->IsChecked()){
PictureListPrefPtr->insert(std::make_pair(PictureListIndex, sliPriority->GetValue()));
} else {
PictureListPrefPtr->insert(std::make_pair(PictureListIndex, 0));
}
// Setup MIME Type.
PictureListPictureTypePtr->insert(std::make_pair(PictureListIndex, wxSPictureMIMEType));
// Setup encoding type.
PictureListPicEncTypePtr->insert(std::make_pair(PictureListIndex, wxT("base64")));
// Add to form.
wxListItem coldata;
coldata.SetId(PictureListIndex);
coldata.SetData(PictureListIndex);
if (PictureLogo == TRUE){
coldata.SetText(_("Picture"));
} else {
coldata.SetText(_("Logo"));
}
ListCtrlIndex = PictureListCtrlPtr->InsertItem(coldata);
if (strValue == _("Home")) {
PictureListCtrlPtr->SetItem(ListCtrlIndex, 1, _("Home"));
} else if (strValue == _("Work")) {
PictureListCtrlPtr->SetItem(ListCtrlIndex, 1, _("Work"));
} else {
}
if (chkUsePref->IsChecked()){
PictureListCtrlPtr->SetItem(ListCtrlIndex, 2, wxString::Format(wxT("%i"), sliPriority->GetValue()));
}
this->Close();
} else if (EditorMode == TRUE) {
// Do not touch the audio file but update the settings
// associated with the audio file.
long longSelected = -1;
wxString strValue;
// Update Picture Type.
PictureListTypePtr->erase(PictureListIndex);
strValue = cmbType->GetString(cmbType->GetSelection());
if (strValue == _("Home")) {
PictureListTypePtr->insert(std::make_pair(PictureListIndex, wxT("home")));
} else if (strValue == _("Work")) {
PictureListTypePtr->insert(std::make_pair(PictureListIndex, wxT("work")));
}
// Update Category Priority.
PictureListPrefPtr->erase(PictureListIndex);
if (chkUsePref->IsChecked()){
PictureListPrefPtr->insert(std::make_pair(PictureListIndex, sliPriority->GetValue()));
} else {
PictureListPrefPtr->insert(std::make_pair(PictureListIndex, 0));
}
// Update form.
longSelected = PictureListCtrlPtr->GetNextItem(longSelected,
wxLIST_NEXT_ALL,
wxLIST_STATE_SELECTED);
if (longSelected == -1){
return;
}
if (PictureLogo == TRUE){
PictureListCtrlPtr->SetItem(longSelected, 0, _("Picture"));
} else {
PictureListCtrlPtr->SetItem(longSelected, 0, _("Logo"));
}
PictureListCtrlPtr->SetItem(longSelected, 1, strValue);
if (chkUsePref->IsChecked()){
PictureListCtrlPtr->SetItem(longSelected, 2, wxString::Format(wxT("%i"), sliPriority->GetValue()));
} else {
PictureListCtrlPtr->SetItem(longSelected, 2, wxT(""));
}
this->Close();
}
}
void frmContactEditorPicture::CloseWindow( wxCommandEvent& event )
{
this->Close();
}
void frmContactEditorPicture::SetEditorMode(bool EditMode)
{
// Set if the editor is adding or editing an address.
// FALSE = Add
// TRUE = Edit
if (EditMode == FALSE){
EditorMode = FALSE;
btnAction->SetLabel(_("Add"));
this->SetTitle(_("Add Picture"));
} else if (EditMode == TRUE){
EditorMode = TRUE;
btnAction->SetLabel(_("Modify"));
this->SetTitle(_("Modify Picture"));
fipPicture->Disable();
std::map::iterator intiter;
std::map::iterator striter;
wxString strValue;
// Load the data into the form. Get the website.
striter = PictureListTypePtr->find(PictureListIndex);
if (striter->first == PictureListIndex){
strValue = striter->second;
}
if (strValue == wxT("home")){
cmbType->SetSelection(1);
} else if (strValue == wxT("work")){
cmbType->SetSelection(2);
} else {
cmbType->SetSelection(0);
}
//strValue.Clear();
// Get the website priority.
intiter = PictureListPrefPtr->find(PictureListIndex);
if (intiter->first == PictureListIndex && intiter->second > 0){
sliPriority->SetValue(intiter->second);
sliPriority->Enable();
chkUsePref->SetValue(TRUE);
}
}
}
void frmContactEditorPicture::SetupPointers(std::map *PictureList,
std::map *PictureListAltID,
std::map *PictureListPID,
std::map *PictureListType,
std::map *PictureListPicEncType,
std::map *PictureListPictureType,
std::map *PictureListTokens,
std::map *PictureListMediatype,
std::map *PictureListPref,
wxListCtrl *PictureListCtrl,
int PictureIndex,
bool PictureLogoIn )
{
PictureListPtr = PictureList;
PictureListAltIDPtr = PictureListAltID;
PictureListPIDPtr = PictureListPID;
PictureListPicEncTypePtr = PictureListPicEncType;
PictureListPictureTypePtr = PictureListPictureType;
PictureListTypePtr = PictureListType;
PictureListTokensPtr = PictureListTokens;
PictureListMediatypePtr = PictureListMediatype;
PictureListPrefPtr = PictureListPref;
PictureListCtrlPtr = PictureListCtrl;
PictureListIndex = PictureIndex;
PictureLogo = PictureLogoIn;
}