1 // mime.cpp - MIME subroutines.
3 // (c) 2012-2015 Xestia Software Development.
5 // This file is part of Xestia Address Book.
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.
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.
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/>
21 wxString GetMIME(wxString FilenameInc){
25 // Check if file exists first before doing anything with it.
27 if (!wxFileExists(FilenameInc)){
29 // File doesn't exist so return.
31 wxMessageBox(_("The file with the filename given doesn't exist."), _("Error opening picture/audio file"), wxICON_ERROR);
36 #if defined(__WIN32__)
38 // TODO: Sort out Win32.
48 // Build Command Line.
50 wxString FilenameIncEscaped = FilenameInc;
51 FilenameIncEscaped.Replace(wxT(" "), wxT("\\ "));
52 wxString FileCommandLine;
54 FileCommandLine.Append("file -b --mime-type ");
55 FileCommandLine.Append(FilenameIncEscaped);
57 if (!(MIMEIn = popen(FileCommandLine.mb_str(), "r"))){
59 wxMessageBox(_("The file with the filename given doesn't exist."), _("Error opening picture/audio file"), wxICON_ERROR);
64 while(fgets(MIMEData, sizeof(MIMEData), MIMEIn) != NULL){
65 MIMEResult.Append(wxString::FromUTF8(MIMEData));
78 magic_t MagicCookie = magic_open(MAGIC_MIME);
79 magic_setflags(MagicCookie, MAGIC_MIME_TYPE);
83 wxMessageBox(_("An error occured with the support library for getting the picture/audio type."), _("Error opening picture/audio file"), wxICON_ERROR);
88 if (magic_load(MagicCookie, NULL) != 0) {
90 wxMessageBox(_("An error occured whilst determining the picture/audio type."), _("Error opening picture/audio file"), wxICON_ERROR);
95 MIMEType = magic_file(MagicCookie, FilenameInc.mb_str());
96 MIMEResult = wxString::FromUTF8(MIMEType);
97 magic_close(MagicCookie);