2 // This file is part of the aMule Project.
4 // Copyright (c) 2003-2008 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2002 Merkur ( devs@emule-project.net / http://www.emule-project.net )
7 // Any parts of this program derived from the xMule, lMule or eMule project,
8 // or contributed by third-party developers are copyrighted by their
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 // The backtrace functions contain modified code from libYaMa, (c) Venkatesha Murthy G.
27 // You can check libYaMa at http://personal.pavanashree.org/libyama/
29 #include <tags/FileTags.h>
31 #include <wx/filename.h> // Needed for wxFileName
32 #include <wx/log.h> // Needed for wxLogNull
35 #include "config.h" // Needed for a number of defines
38 #include <wx/stdpaths.h> // Do_not_auto_remove
39 #include <common/StringFunctions.h>
40 #include <common/ClientVersion.h>
41 #include <common/MD5Sum.h>
42 #include <common/Path.h>
46 #include "OtherFunctions.h" // Interface declarations
57 wxString
GetMuleVersion()
59 wxString
ver(wxT(VERSION
));
61 ver
+= wxT(" using ");
64 // Figure out the wx build-type
71 #elif defined(__WXMAC__)
73 #elif defined(__WXMSW__)
75 #elif defined(__WXCOCOA__)
76 ver
+= wxT("wxCocoa");
79 ver
+= wxString::Format(wxT(" v%d.%d.%d"), wxMAJOR_VERSION
, wxMINOR_VERSION
, wxRELEASE_NUMBER
);
81 #if defined(__WXDEBUG__)
82 ver
+= wxT(" (Debugging)");
86 ver
+= wxString::Format( wxT(" (Snapshot: %s)"), wxT(SVNDATE
));
93 wxString
GetFullMuleVersion()
96 wxString app
= wxT("aMuled");
97 #elif defined(CLIENT_GUI)
98 wxString app
= wxT("Remote aMule-GUI");
100 wxString app
= wxT("aMule");
103 return app
+ wxT(" ") + GetMuleVersion();
107 // Formats a filesize in bytes to make it suitable for displaying
108 wxString
CastItoXBytes( uint64 count
)
112 return wxString::Format( wxT("%.0f "), (float)(uint32
)count
) + wxPLURAL("byte", "bytes", count
) ;
113 else if (count
< 1048576)
114 return wxString::Format( wxT("%.0f "), (float)(uint32
)count
/1024) + _("kB") ;
115 else if (count
< 1073741824)
116 return wxString::Format( wxT("%.2f "), (float)(uint32
)count
/1048576) + _("MB") ;
117 else if (count
< 1099511627776LL)
118 return wxString::Format( wxT("%.2f "), (float)((uint32
)(count
/1024))/1048576) + _("GB") ;
120 return wxString::Format( wxT("%.3f "), (float)count
/1099511627776LL) + _("TB") ;
126 wxString
CastItoIShort(uint64 count
)
130 return wxString::Format(wxT("%u"), (uint32
)count
);
131 else if (count
< 1000000)
132 return wxString::Format(wxT("%.0f"),(float)(uint32
)count
/1000) + _("k") ;
133 else if (count
< 1000000000)
134 return wxString::Format(wxT("%.2f"),(float)(uint32
)count
/1000000) + _("M") ;
135 else if (count
< 1000000000000LL)
136 return wxString::Format(wxT("%.2f"),(float)((uint32
)(count
/1000))/1000000) + _("G") ;
137 else if (count
< 1000000000000000LL)
138 return wxString::Format(wxT("%.2f"),(float)count
/1000000000000LL) + _("T");
144 wxString
CastItoSpeed(uint32 bytes
)
147 return wxString::Format(wxT("%u "), bytes
) + wxPLURAL("byte/sec", "bytes/sec", bytes
);
148 else if (bytes
< 1048576)
149 return wxString::Format(wxT("%.2f "), bytes
/ 1024.0) + _("kB/s");
151 return wxString::Format(wxT("%.2f "), bytes
/ 1048576.0) + _("MB/s");
155 // Make a time value in seconds suitable for displaying
156 wxString
CastSecondsToHM(uint64 count
, uint16 msecs
)
160 return wxString::Format(
161 wxT("%02") wxLongLongFmtSpec
wxT("u "),
164 return wxString::Format(
166 (count
+ ((float)msecs
/1000))) + _("secs");
168 } else if (count
< 3600) {
169 return wxString::Format(
171 wxLongLongFmtSpec
wxT("u:%02")
172 wxLongLongFmtSpec
wxT("u "),
174 (count
% 60)) + _("mins");
175 } else if (count
< 86400) {
176 return wxString::Format(
178 wxLongLongFmtSpec
wxT("u:%02")
179 wxLongLongFmtSpec
wxT("u "),
181 (count
% 3600)/60) + _("hours");
183 return wxString::Format(
185 wxLongLongFmtSpec
wxT("u %s %02")
186 wxLongLongFmtSpec
wxT("u:%02")
187 wxLongLongFmtSpec
wxT("u "),
190 (count
% 86400)/3600,
191 (count
% 3600)/60) + _("hours");
198 // Examines a filename and determines the filetype
199 FileType
GetFiletype(const CPath
& filename
)
201 // FIXME: WTF do we have two such functions in the first place?
202 switch (GetED2KFileTypeID(filename
)) {
203 case ED2KFT_AUDIO
: return ftAudio
;
204 case ED2KFT_VIDEO
: return ftVideo
;
205 case ED2KFT_IMAGE
: return ftPicture
;
206 case ED2KFT_PROGRAM
: return ftProgram
;
207 case ED2KFT_DOCUMENT
: return ftText
;
208 case ED2KFT_ARCHIVE
: return ftArchive
;
209 case ED2KFT_CDIMAGE
: return ftCDImage
;
210 default: return ftAny
;
215 // Returns the (translated) description assosiated with a FileType
216 wxString
GetFiletypeDesc(FileType type
, bool translated
)
223 return wxT("Videos");
235 return _("Archives");
237 return wxT("Archives");
242 return _("CD-Images");
244 return wxT("CD-Images");
249 return _("Pictures");
251 return wxT("Pictures");
263 return _("Programs");
265 return wxT("Programs");
278 // Returns the Typename, examining the extention of the given filename
280 wxString
GetFiletypeByName(const CPath
& filename
, bool translated
)
282 return GetFiletypeDesc(GetFiletype(filename
), translated
);
286 // Return the text associated with a rating of a file
287 wxString
GetRateString(uint16 rate
)
290 case 0: return _("Not rated");
291 case 1: return _("Invalid / Corrupt / Fake");
292 case 2: return _("Poor");
293 case 3: return _("Fair");
294 case 4: return _("Good");
295 case 5: return _("Excellent");
296 default: return _("Not rated");
302 * Return the size in bytes of the given size-type
304 * @param type The type (as an int) where: 0 = Byte, 1 = KB, 2 = MB, 3 = GB
306 * @return The amount of Bytes the provided size-type represents
308 * Values over GB aren't handled since the amount of Bytes 1TB represents
309 * is over the uint32 capacity
311 uint32
GetTypeSize(uint8 type
)
313 enum {Bytes
, KB
, MB
, GB
};
317 case Bytes
: size
= 1; break;
318 case KB
: size
= 1024; break;
319 case MB
: size
= 1048576; break;
320 case GB
: size
= 1073741824; break;
321 default: size
= -1; break;
327 // Base16 chars for encode an decode functions
328 static wxChar base16Chars
[17] = wxT("0123456789ABCDEF");
329 static wxChar base32Chars
[33] = wxT("ABCDEFGHIJKLMNOPQRSTUVWXYZ234567");
330 #define BASE16_LOOKUP_MAX 23
331 static wxChar base16Lookup
[BASE16_LOOKUP_MAX
][2] = {
358 // Returns a BASE16 encoded byte array
361 // buffer: Pointer to byte array
362 // bufLen: Lenght of buffer array
365 // wxString object with BASE16 encoded byte array
366 wxString
EncodeBase16(const unsigned char* buffer
, unsigned int bufLen
)
370 for(unsigned int i
= 0; i
< bufLen
; ++i
) {
371 Base16Buff
+= base16Chars
[buffer
[i
] >> 4];
372 Base16Buff
+= base16Chars
[buffer
[i
] & 0xf];
379 // Decodes a BASE16 string into a byte array
382 // base16Buffer: String containing BASE16
383 // base16BufLen: Lenght BASE16 coded string's length
386 // buffer: byte array containing decoded string
387 unsigned int DecodeBase16(const wxString
&base16Buffer
, unsigned int base16BufLen
, byte
*buffer
)
389 if (base16BufLen
& 1) {
392 unsigned int ret
= base16BufLen
>> 1;
393 memset( buffer
, 0, ret
);
394 for(unsigned int i
= 0; i
< base16BufLen
; ++i
) {
395 int lookup
= toupper(base16Buffer
[i
]) - wxT('0');
396 // Check to make sure that the given word falls inside a valid range
397 byte word
= (lookup
< 0 || lookup
>= BASE16_LOOKUP_MAX
) ?
398 0xFF : base16Lookup
[lookup
][1];
399 unsigned idx
= i
>> 1;
400 buffer
[idx
] = (i
& 1) ? // odd or even?
401 (buffer
[idx
] | word
) : (word
<< 4);
408 // Returns a BASE32 encoded byte array
411 // buffer: Pointer to byte array
412 // bufLen: Lenght of buffer array
415 // wxString object with BASE32 encoded byte array
416 wxString
EncodeBase32(const unsigned char* buffer
, unsigned int bufLen
)
419 unsigned int i
, index
;
422 for(i
= 0, index
= 0; i
< bufLen
;) {
423 // Is the current word going to span a byte boundary?
425 word
= (buffer
[i
] & (0xFF >> index
));
426 index
= (index
+ 5) % 8;
428 if (i
< bufLen
- 1) {
429 word
|= buffer
[i
+ 1] >> (8 - index
);
433 word
= (buffer
[i
] >> (8 - (index
+ 5))) & 0x1F;
434 index
= (index
+ 5) % 8;
439 Base32Buff
+= (char) base32Chars
[word
];
446 // Decodes a BASE32 string into a byte array
449 // base32Buffer: String containing BASE32
450 // base32BufLen: Lenght BASE32 coded string's length
453 // buffer: byte array containing decoded string
456 unsigned int DecodeBase32(const wxString
&base32Buffer
, unsigned int base32BufLen
, unsigned char *buffer
)
458 size_t nInputLen
= base32Buffer
.Length();
459 uint32 nDecodeLen
= (nInputLen
* 5) / 8;
460 if ((nInputLen
* 5) % 8 > 0) {
463 if (base32BufLen
== 0) {
466 if (nDecodeLen
> base32BufLen
) {
472 for (size_t i
= 0; i
< nInputLen
; ++i
)
474 if (base32Buffer
[i
] >= wxT('A') && base32Buffer
[i
] <= wxT('Z')) {
475 nBits
|= ( base32Buffer
[i
] - wxT('A') );
477 else if (base32Buffer
[i
] >= wxT('a') && base32Buffer
[i
] <= wxT('z')) {
478 nBits
|= ( base32Buffer
[i
] - wxT('a') );
480 else if (base32Buffer
[i
] >= wxT('2') && base32Buffer
[i
] <= wxT('7')) {
481 nBits
|= ( base32Buffer
[i
] - wxT('2') + 26 );
488 *buffer
++ = (byte
)( nBits
>> (nCount
- 8) );
501 * Base64 encoding/decoding command line filter
503 * Copyright (c) 2002 Matthias Gaertner 29.06.2002
504 * Adapted by (C) 2005-2008 Phoenix to use wxWidgets.
507 static const wxString
to_b64(
508 /* 0000000000111111111122222222223333333333444444444455555555556666 */
509 /* 0123456789012345678901234567890123456789012345678901234567890123 */
510 wxT("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"));
513 /* Option variables */
514 static bool g_fUseCRLF
= false;
515 static unsigned int g_nCharsPerLine
= 72;
516 static wxString strHeaderLine
;
519 wxString
EncodeBase64(const char *pbBufferIn
, unsigned int bufLen
)
521 wxString pbBufferOut
;
524 if( !strHeaderLine
.IsEmpty() ) {
525 strHeader
= wxT("-----BEGIN ") + strHeaderLine
+ wxT("-----");
527 strHeader
+= wxT("\r");
529 strHeader
+= wxT("\n");
532 unsigned long nDiv
= ((unsigned long)bufLen
) / 3;
533 unsigned long nRem
= ((unsigned long)bufLen
) % 3;
534 unsigned int NewLineSize
= g_fUseCRLF
? 2 : 1;
536 // Allocate enough space in the output buffer to speed up things
538 strHeader
.Len() * 2 + // header/footer
539 (bufLen
* 4) / 3 + 1 + // Number of codes
540 nDiv
* NewLineSize
+ // Number of new lines
541 (nRem
? 1 : 0) * NewLineSize
); // Last line
542 pbBufferOut
= strHeader
;
544 unsigned long nChars
= 0;
545 const unsigned char *pIn
= (unsigned char*)pbBufferIn
;
547 pbBufferOut
+= to_b64
[ (pIn
[0] >> 2) & 0x3f];
548 pbBufferOut
+= to_b64
[((pIn
[0] << 4) & 0x30) | ((pIn
[1] >> 4) & 0xf)];
549 pbBufferOut
+= to_b64
[((pIn
[1] << 2) & 0x3c) | ((pIn
[2] >> 6) & 0x3)];
550 pbBufferOut
+= to_b64
[ pIn
[2] & 0x3f];
554 if( nChars
>= g_nCharsPerLine
&& g_nCharsPerLine
!= 0 ) {
557 pbBufferOut
+= wxT("\r");
559 pbBufferOut
+= wxT("\n");
564 pbBufferOut
+= to_b64
[ (pIn
[0] >> 2) & 0x3f];
565 pbBufferOut
+= to_b64
[((pIn
[0] << 4) & 0x30) | ((pIn
[1] >> 4) & 0xf)];
566 pbBufferOut
+= to_b64
[ (pIn
[1] << 2) & 0x3c];
567 pbBufferOut
+= wxT("=");
569 if( nChars
>= g_nCharsPerLine
&& g_nCharsPerLine
!= 0 ) {
572 pbBufferOut
+= wxT("\r");
574 pbBufferOut
+= wxT("\n");
578 pbBufferOut
+= to_b64
[ (pIn
[0] >> 2) & 0x3f];
579 pbBufferOut
+= to_b64
[ (pIn
[0] << 4) & 0x30];
580 pbBufferOut
+= wxT("=");
581 pbBufferOut
+= wxT("=");
583 if( nChars
>= g_nCharsPerLine
&& g_nCharsPerLine
!= 0 ) {
586 pbBufferOut
+= wxT("\r");
588 pbBufferOut
+= wxT("\n");
596 pbBufferOut
+= wxT("\r");
598 pbBufferOut
+= wxT("\n");
602 if( !strHeaderLine
.IsEmpty() ) {
603 pbBufferOut
= wxT("-----END ") + strHeaderLine
+ wxT("-----");
605 pbBufferOut
+= wxT("\r");
607 pbBufferOut
+= wxT("\n");
614 unsigned int DecodeBase64(const wxString
&base64Buffer
, unsigned int base64BufLen
, unsigned char *buffer
)
616 int z
= 0; // 0 Normal, 1 skip MIME separator (---) to end of line
617 unsigned int nData
= 0;
620 if (base64BufLen
== 0) {
625 for(unsigned int j
= 0; j
< base64BufLen
; ++j
) {
626 wxChar c
= base64Buffer
[j
];
627 wxChar bits
= wxT('z');
633 else if(c
>= wxT('A') && c
<= wxT('Z')) {
636 else if(c
>= wxT('a') && c
<= wxT('z')) {
637 bits
= c
- wxT('a') + (wxChar
)26;
639 else if(c
>= wxT('0') && c
<= wxT('9')) {
640 bits
= c
- wxT('0') + (wxChar
)52;
642 else if(c
== wxT('+')) {
645 else if(c
== wxT('/')) {
648 else if(c
== wxT('-')) {
651 else if(c
== wxT('=')) {
657 // Skips anything that was not recognized
658 // as a base64 valid char ('y' or 'z')
659 if (bits
< (wxChar
)64) {
662 buffer
[i
+0] = (bits
<< 2) & 0xfc;
665 buffer
[i
+0] |= (bits
>> 4) & 0x03;
666 buffer
[i
+1] = (bits
<< 4) & 0xf0;
669 buffer
[i
+1] |= (bits
>> 2) & 0x0f;
670 buffer
[i
+2] = (bits
<< 6) & 0xc0;
673 buffer
[i
+2] |= bits
& 0x3f;
683 // Syntax error or buffer was empty
695 // Returns the text assosiated with a category type
696 wxString
GetCatTitle(int catid
)
699 case 0: return _("all");
700 case 1: return _("all others");
701 case 2: return _("Incomplete");
702 case 3: return _("Completed");
703 case 4: return _("Waiting");
704 case 5: return _("Downloading");
705 case 6: return _("Erroneous");
706 case 7: return _("Paused");
707 case 8: return _("Stopped");
708 case 9: return _("Video");
709 case 10: return _("Audio");
710 case 11: return _("Archive");
711 case 12: return _("CD-Images");
712 case 13: return _("Pictures");
713 case 14: return _("Text");
714 case 15: return _("Active");
715 default: return wxT("?");
720 typedef std::map
<wxString
, EED2KFileTypeClass
> SED2KFileTypeMap
;
721 typedef SED2KFileTypeMap::value_type SED2KFileTypeMapElement
;
722 static SED2KFileTypeMap ED2KFileTypesMap
;
725 class CED2KFileTypes
{
729 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".669"), ED2KFT_AUDIO
));
730 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".aac"), ED2KFT_AUDIO
));
731 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".aif"), ED2KFT_AUDIO
));
732 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".aiff"), ED2KFT_AUDIO
));
733 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".amf"), ED2KFT_AUDIO
));
734 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".ams"), ED2KFT_AUDIO
));
735 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".ape"), ED2KFT_AUDIO
));
736 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".au"), ED2KFT_AUDIO
));
737 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".dbm"), ED2KFT_AUDIO
));
738 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".dmf"), ED2KFT_AUDIO
));
739 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".dsm"), ED2KFT_AUDIO
));
740 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".far"), ED2KFT_AUDIO
));
741 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".flac"), ED2KFT_AUDIO
));
742 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".it"), ED2KFT_AUDIO
));
743 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mdl"), ED2KFT_AUDIO
));
744 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".med"), ED2KFT_AUDIO
));
745 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mid"), ED2KFT_AUDIO
));
746 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".midi"), ED2KFT_AUDIO
));
747 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mod"), ED2KFT_AUDIO
));
748 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mol"), ED2KFT_AUDIO
));
749 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mp1"), ED2KFT_AUDIO
));
750 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mp2"), ED2KFT_AUDIO
));
751 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mp3"), ED2KFT_AUDIO
));
752 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mp4"), ED2KFT_AUDIO
));
753 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mpa"), ED2KFT_AUDIO
));
754 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mpc"), ED2KFT_AUDIO
));
755 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mpp"), ED2KFT_AUDIO
));
756 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mtm"), ED2KFT_AUDIO
));
757 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".nst"), ED2KFT_AUDIO
));
758 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".ogg"), ED2KFT_AUDIO
));
759 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".okt"), ED2KFT_AUDIO
));
760 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".psm"), ED2KFT_AUDIO
));
761 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".ptm"), ED2KFT_AUDIO
));
762 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".ra"), ED2KFT_AUDIO
));
763 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".rmi"), ED2KFT_AUDIO
));
764 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".s3m"), ED2KFT_AUDIO
));
765 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".stm"), ED2KFT_AUDIO
));
766 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".ult"), ED2KFT_AUDIO
));
767 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".umx"), ED2KFT_AUDIO
));
768 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".wav"), ED2KFT_AUDIO
));
769 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".wma"), ED2KFT_AUDIO
));
770 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".wow"), ED2KFT_AUDIO
));
771 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".xm"), ED2KFT_AUDIO
));
773 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".asf"), ED2KFT_VIDEO
));
774 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".avi"), ED2KFT_VIDEO
));
775 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".divx"), ED2KFT_VIDEO
));
776 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".m1v"), ED2KFT_VIDEO
));
777 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".m2v"), ED2KFT_VIDEO
));
778 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mkv"), ED2KFT_VIDEO
));
779 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mov"), ED2KFT_VIDEO
));
780 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mp1v"), ED2KFT_VIDEO
));
781 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mp2v"), ED2KFT_VIDEO
));
782 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mpe"), ED2KFT_VIDEO
));
783 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mpeg"), ED2KFT_VIDEO
));
784 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mpg"), ED2KFT_VIDEO
));
785 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mps"), ED2KFT_VIDEO
));
786 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mpv"), ED2KFT_VIDEO
));
787 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mpv1"), ED2KFT_VIDEO
));
788 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mpv2"), ED2KFT_VIDEO
));
789 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".ogm"), ED2KFT_VIDEO
));
790 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".qt"), ED2KFT_VIDEO
));
791 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".ram"), ED2KFT_VIDEO
));
792 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".rm"), ED2KFT_VIDEO
));
793 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".rv"), ED2KFT_VIDEO
));
794 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".rv9"), ED2KFT_VIDEO
));
795 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".ts"), ED2KFT_VIDEO
));
796 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".vivo"), ED2KFT_VIDEO
));
797 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".vob"), ED2KFT_VIDEO
));
798 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".wmv"), ED2KFT_VIDEO
));
799 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".xvid"), ED2KFT_VIDEO
));
801 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".bmp"), ED2KFT_IMAGE
));
802 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".dcx"), ED2KFT_IMAGE
));
803 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".emf"), ED2KFT_IMAGE
));
804 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".gif"), ED2KFT_IMAGE
));
805 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".ico"), ED2KFT_IMAGE
));
806 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".jpeg"), ED2KFT_IMAGE
));
807 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".jpg"), ED2KFT_IMAGE
));
808 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".pct"), ED2KFT_IMAGE
));
809 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".pcx"), ED2KFT_IMAGE
));
810 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".pic"), ED2KFT_IMAGE
));
811 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".pict"), ED2KFT_IMAGE
));
812 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".png"), ED2KFT_IMAGE
));
813 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".psd"), ED2KFT_IMAGE
));
814 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".psp"), ED2KFT_IMAGE
));
815 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".tga"), ED2KFT_IMAGE
));
816 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".tif"), ED2KFT_IMAGE
));
817 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".tiff"), ED2KFT_IMAGE
));
818 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".wmf"), ED2KFT_IMAGE
));
819 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".xif"), ED2KFT_IMAGE
));
821 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".7z"), ED2KFT_ARCHIVE
));
822 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".ace"), ED2KFT_ARCHIVE
));
823 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".arj"), ED2KFT_ARCHIVE
));
824 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".bz2"), ED2KFT_ARCHIVE
));
825 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".cab"), ED2KFT_ARCHIVE
));
826 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".gz"), ED2KFT_ARCHIVE
));
827 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".hqx"), ED2KFT_ARCHIVE
));
828 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".lha"), ED2KFT_ARCHIVE
));
829 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".msi"), ED2KFT_ARCHIVE
));
830 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".rar"), ED2KFT_ARCHIVE
));
831 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".sea"), ED2KFT_ARCHIVE
));
832 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".sit"), ED2KFT_ARCHIVE
));
833 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".tar"), ED2KFT_ARCHIVE
));
834 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".tgz"), ED2KFT_ARCHIVE
));
835 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".uc2"), ED2KFT_ARCHIVE
));
836 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".zip"), ED2KFT_ARCHIVE
));
838 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".bat"), ED2KFT_PROGRAM
));
839 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".cmd"), ED2KFT_PROGRAM
));
840 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".com"), ED2KFT_PROGRAM
));
841 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".exe"), ED2KFT_PROGRAM
));
843 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".bin"), ED2KFT_CDIMAGE
));
844 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".bwa"), ED2KFT_CDIMAGE
));
845 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".bwi"), ED2KFT_CDIMAGE
));
846 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".bws"), ED2KFT_CDIMAGE
));
847 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".bwt"), ED2KFT_CDIMAGE
));
848 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".ccd"), ED2KFT_CDIMAGE
));
849 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".cue"), ED2KFT_CDIMAGE
));
850 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".dmg"), ED2KFT_CDIMAGE
));
851 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".dmz"), ED2KFT_CDIMAGE
));
852 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".img"), ED2KFT_CDIMAGE
));
853 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".iso"), ED2KFT_CDIMAGE
));
854 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mdf"), ED2KFT_CDIMAGE
));
855 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mds"), ED2KFT_CDIMAGE
));
856 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".nrg"), ED2KFT_CDIMAGE
));
857 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".sub"), ED2KFT_CDIMAGE
));
858 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".toast"), ED2KFT_CDIMAGE
));
860 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".chm"), ED2KFT_DOCUMENT
));
861 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".css"), ED2KFT_DOCUMENT
));
862 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".diz"), ED2KFT_DOCUMENT
));
863 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".doc"), ED2KFT_DOCUMENT
));
864 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".dot"), ED2KFT_DOCUMENT
));
865 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".hlp"), ED2KFT_DOCUMENT
));
866 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".htm"), ED2KFT_DOCUMENT
));
867 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".html"), ED2KFT_DOCUMENT
));
868 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".nfo"), ED2KFT_DOCUMENT
));
869 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".pdf"), ED2KFT_DOCUMENT
));
870 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".pps"), ED2KFT_DOCUMENT
));
871 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".ppt"), ED2KFT_DOCUMENT
));
872 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".ps"), ED2KFT_DOCUMENT
));
873 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".rtf"), ED2KFT_DOCUMENT
));
874 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".wri"), ED2KFT_DOCUMENT
));
875 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".txt"), ED2KFT_DOCUMENT
));
876 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".xls"), ED2KFT_DOCUMENT
));
877 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".xlt"), ED2KFT_DOCUMENT
));
882 // get the list initialized *before* any code is accessing it
883 CED2KFileTypes theED2KFileTypes
;
885 EED2KFileType
GetED2KFileTypeID(const CPath
& fileName
)
887 const wxString ext
= fileName
.GetExt().Lower();
892 SED2KFileTypeMap::iterator it
= ED2KFileTypesMap
.find(wxT(".") + ext
);
893 if (it
!= ED2KFileTypesMap
.end()) {
894 return it
->second
.GetType();
901 // Retuns the ed2k file type term which is to be used in server searches
902 wxString
GetED2KFileTypeSearchTerm(EED2KFileType iFileID
)
904 if (iFileID
== ED2KFT_AUDIO
) return ED2KFTSTR_AUDIO
;
905 if (iFileID
== ED2KFT_VIDEO
) return ED2KFTSTR_VIDEO
;
906 if (iFileID
== ED2KFT_IMAGE
) return ED2KFTSTR_IMAGE
;
907 if (iFileID
== ED2KFT_DOCUMENT
) return ED2KFTSTR_DOCUMENT
;
908 if (iFileID
== ED2KFT_PROGRAM
) return ED2KFTSTR_PROGRAM
;
909 // NOTE: Archives and CD-Images are published with file type "Pro"
910 if (iFileID
== ED2KFT_ARCHIVE
) return ED2KFTSTR_PROGRAM
;
911 if (iFileID
== ED2KFT_CDIMAGE
) return ED2KFTSTR_PROGRAM
;
913 return wxEmptyString
;
917 // Returns a file type which is used eMule internally only, examining the extention of the given filename
918 wxString
GetFileTypeByName(const CPath
& fileName
)
920 EED2KFileType iFileType
= GetED2KFileTypeID(fileName
);
922 case ED2KFT_AUDIO
: return ED2KFTSTR_AUDIO
;
923 case ED2KFT_VIDEO
: return ED2KFTSTR_VIDEO
;
924 case ED2KFT_IMAGE
: return ED2KFTSTR_IMAGE
;
925 case ED2KFT_DOCUMENT
: return ED2KFTSTR_DOCUMENT
;
926 case ED2KFT_PROGRAM
: return ED2KFTSTR_PROGRAM
;
927 case ED2KFT_ARCHIVE
: return ED2KFTSTR_ARCHIVE
;
928 case ED2KFT_CDIMAGE
: return ED2KFTSTR_CDIMAGE
;
929 default: return wxEmptyString
;
934 // Retuns the ed2k file type integer ID which is to be used for publishing+searching
935 EED2KFileType
GetED2KFileTypeSearchID(EED2KFileType iFileID
)
938 case ED2KFT_AUDIO
: return ED2KFT_AUDIO
;
939 case ED2KFT_VIDEO
: return ED2KFT_VIDEO
;
940 case ED2KFT_IMAGE
: return ED2KFT_IMAGE
;
941 case ED2KFT_DOCUMENT
: return ED2KFT_DOCUMENT
;
942 case ED2KFT_PROGRAM
: return ED2KFT_PROGRAM
;
943 // NOTE: Archives and CD-Images are published+searched with file type "Pro"
944 // NOTE: If this gets changed, the function 'GetED2KFileTypeSearchTerm' also needs to get updated!
945 case ED2KFT_ARCHIVE
: return ED2KFT_PROGRAM
;
946 case ED2KFT_CDIMAGE
: return ED2KFT_PROGRAM
;
947 default: return ED2KFT_ANY
;
953 * Dumps a buffer to a wxString
955 wxString
DumpMemToStr(const void *buff
, int n
, const wxString
& msg
, bool ok
)
957 const unsigned char *p
= (const unsigned char *)buff
;
958 int lines
= (n
+ 15)/ 16;
961 // Allocate aproximetly what is needed
962 result
.Alloc( ( lines
+ 1 ) * 80 );
963 if ( !msg
.IsEmpty() ) {
964 result
+= msg
+ wxT(" - ok=") + ( ok
? wxT("true, ") : wxT("false, ") );
967 result
+= wxString::Format( wxT("%d bytes\n"), n
);
968 for ( int i
= 0; i
< lines
; ++i
) {
970 result
+= wxString::Format( wxT("%08x "), i
* 16 );
972 // Show two columns of hex-values
973 for ( int j
= 0; j
< 2; ++j
) {
974 for ( int k
= 0; k
< 8; ++k
) {
975 int pos
= 16 * i
+ 8 * j
+ k
;
978 result
+= wxString::Format( wxT("%02x "), p
[pos
] );
986 // Show a column of ascii-values
987 for ( int k
= 0; k
< 16; ++k
) {
988 int pos
= 16 * i
+ k
;
991 if ( isspace( p
[pos
] ) ) {
993 } else if ( !isgraph( p
[pos
] ) ) {
996 result
+= (wxChar
)p
[pos
];
1002 result
+= wxT("|\n");
1011 * Dumps a buffer to stdout
1013 void DumpMem(const void *buff
, int n
, const wxString
& msg
, bool ok
)
1015 printf("%s\n", (const char*)unicode2char(DumpMemToStr( buff
, n
, msg
, ok
)) );
1020 // Dump mem in dword format
1021 void DumpMem_DW(const uint32
*ptr
, int count
)
1023 for(int i
= 0; i
< count
; i
++) {
1024 printf("%08x ", ptr
[i
]);
1025 if ( (i
% 4) == 3) printf("\n");
1031 wxString
GetConfigDir()
1034 static wxString configPath
;
1036 if (configPath
.IsEmpty()) {
1038 // "Portable aMule" - Use aMule from an external USB drive
1039 // Check for ./config/amule.conf and use this configuration if found
1040 const wxString configDir
= JoinPaths(wxFileName::GetCwd(), wxT("config"));
1041 const wxString configFile
= JoinPaths(configDir
, wxT("amule.conf"));
1043 if (CPath::DirExists(configDir
) && CPath::FileExists(configFile
)) {
1044 AddLogLineM(true, CFormat(wxT("Using configDir: %s")) % configDir
);
1046 configPath
= configDir
;
1048 configPath
= wxStandardPaths::Get().GetUserDataDir();
1051 configPath
= wxStandardPaths::Get().GetUserDataDir();
1054 configPath
+= wxFileName::GetPathSeparator();
1061 void InitCustomLanguages()
1063 wxLanguageInfo CustomLanguage
;
1064 CustomLanguage
.Language
= wxLANGUAGE_ITALIAN_NAPOLITAN
;
1065 CustomLanguage
.CanonicalName
= wxT("it_NA");
1066 CustomLanguage
.Description
= wxT("sNeo's Custom Napolitan Language");
1067 wxLocale::AddLanguage(CustomLanguage
);
1071 void InitLocale(wxLocale
& locale
, int language
)
1073 int language_flags
= 0;
1074 if (language
!= wxLANGUAGE_CUSTOM
&& language
!= wxLANGUAGE_ITALIAN_NAPOLITAN
) {
1075 language_flags
= wxLOCALE_LOAD_DEFAULT
| wxLOCALE_CONV_ENCODING
;
1078 locale
.Init(language
,language_flags
);
1080 if (language
!= wxLANGUAGE_CUSTOM
) {
1082 #if defined(__WXMAC__) || defined(__WXMSW__)
1083 locale
.AddCatalogLookupPathPrefix(JoinPaths(wxStandardPaths::Get().GetDataDir(), wxT("locale")));
1085 locale
.AddCatalog(wxT(PACKAGE
));
1088 locale
.AddCatalogLookupPathPrefix(GetConfigDir());
1089 locale
.AddCatalog(wxT("custom"));
1094 int StrLang2wx(const wxString
& language
)
1096 // get rid of possible encoding and modifier
1097 wxString
lang(language
.BeforeFirst('.').BeforeFirst('@'));
1099 if (!lang
.IsEmpty()) {
1100 const wxLanguageInfo
*lng
= wxLocale::FindLanguageInfo(lang
);
1102 return lng
->Language
;
1104 return wxLANGUAGE_DEFAULT
;
1107 return wxLANGUAGE_DEFAULT
;
1112 wxString
wxLang2Str(const int lang
)
1114 if (lang
!= wxLANGUAGE_DEFAULT
) {
1115 const wxLanguageInfo
*lng
= wxLocale::GetLanguageInfo(lang
);
1117 return lng
->CanonicalName
;
1119 return wxEmptyString
;
1122 return wxEmptyString
;
1126 wxString
GetPassword() {
1127 wxString pass_plain
;
1130 pass_plain
= char2unicode(getpass("Enter password for mule connection: "));
1132 //#warning This way, pass enter is not hidden on windows. Bad thing.
1135 printf("Enter password for mule connection: \n");
1137 fgets(temp_str
, 512, stdin
);
1138 temp_str
[strlen(temp_str
)-1] = '\0';
1139 pass_plain
= char2unicode(temp_str
);
1141 wxCHECK2(password
.Decode(MD5Sum(pass_plain
).GetHash()), /* Do nothing. */ );
1142 // MD5 hash for an empty string, according to rfc1321.
1143 if (password
.Encode() == wxT("D41D8CD98F00B204E9800998ECF8427E")) {
1144 printf("No empty password allowed.\n");
1145 return GetPassword();
1149 return password
.Encode();
1152 // File_checked_for_headers