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>
32 #include <wx/filename.h> // Needed for wxFileName
33 #include <wx/log.h> // Needed for wxLogNull
36 #include <wx/msw/registry.h> // Do_not_auto_remove
40 #include "config.h" // Needed for a number of defines
43 #include <wx/stdpaths.h> // Do_not_auto_remove
44 #include <common/StringFunctions.h>
45 #include <common/ClientVersion.h>
46 #include <common/MD5Sum.h>
47 #include <common/Path.h>
51 #include "OtherFunctions.h" // Interface declarations
62 wxString
GetMuleVersion()
64 wxString
ver(wxT(VERSION
));
66 ver
+= wxT(" using ");
69 // Figure out the wx build-type
76 #elif defined(__WXMAC__)
78 #elif defined(__WXMSW__)
80 #elif defined(__WXCOCOA__)
81 ver
+= wxT("wxCocoa");
84 ver
+= wxString::Format(wxT(" v%d.%d.%d"), wxMAJOR_VERSION
, wxMINOR_VERSION
, wxRELEASE_NUMBER
);
86 #if defined(__WXDEBUG__)
87 ver
+= wxT(" (Debugging)");
91 ver
+= wxString::Format( wxT(" (Snapshot: %s)"), wxT(SVNDATE
));
98 wxString
GetFullMuleVersion()
101 wxString app
= wxT("aMuled");
102 #elif defined(CLIENT_GUI)
103 wxString app
= wxT("Remote aMule-GUI");
105 wxString app
= wxT("aMule");
108 return app
+ wxT(" ") + GetMuleVersion();
112 // Formats a filesize in bytes to make it suitable for displaying
113 wxString
CastItoXBytes( uint64 count
)
117 return wxString::Format( wxT("%.0f "), (float)(uint32
)count
) + wxPLURAL("byte", "bytes", count
) ;
118 else if (count
< 1048576)
119 return wxString::Format( wxT("%.0f "), (float)(uint32
)count
/1024) + _("kB") ;
120 else if (count
< 1073741824)
121 return wxString::Format( wxT("%.2f "), (float)(uint32
)count
/1048576) + _("MB") ;
122 else if (count
< 1099511627776LL)
123 return wxString::Format( wxT("%.2f "), (float)((uint32
)(count
/1024))/1048576) + _("GB") ;
125 return wxString::Format( wxT("%.3f "), (float)count
/1099511627776LL) + _("TB") ;
131 wxString
CastItoIShort(uint64 count
)
135 return wxString::Format(wxT("%u"), (uint32
)count
);
136 else if (count
< 1000000)
137 return wxString::Format(wxT("%.0f"),(float)(uint32
)count
/1000) + _("k") ;
138 else if (count
< 1000000000)
139 return wxString::Format(wxT("%.2f"),(float)(uint32
)count
/1000000) + _("M") ;
140 else if (count
< 1000000000000LL)
141 return wxString::Format(wxT("%.2f"),(float)((uint32
)(count
/1000))/1000000) + _("G") ;
142 else if (count
< 1000000000000000LL)
143 return wxString::Format(wxT("%.2f"),(float)count
/1000000000000LL) + _("T");
149 wxString
CastItoSpeed(uint32 bytes
)
152 return wxString::Format(wxT("%u "), bytes
) + wxPLURAL("byte/sec", "bytes/sec", bytes
);
153 else if (bytes
< 1048576)
154 return wxString::Format(wxT("%.2f "), bytes
/ 1024.0) + _("kB/s");
156 return wxString::Format(wxT("%.2f "), bytes
/ 1048576.0) + _("MB/s");
160 // Make a time value in seconds suitable for displaying
161 wxString
CastSecondsToHM(uint64 count
, uint16 msecs
)
165 return wxString::Format(
166 wxT("%02") wxLongLongFmtSpec
wxT("u "),
169 return wxString::Format(
171 (count
+ ((float)msecs
/1000))) + _("secs");
173 } else if (count
< 3600) {
174 return wxString::Format(
176 wxLongLongFmtSpec
wxT("u:%02")
177 wxLongLongFmtSpec
wxT("u "),
179 (count
% 60)) + _("mins");
180 } else if (count
< 86400) {
181 return wxString::Format(
183 wxLongLongFmtSpec
wxT("u:%02")
184 wxLongLongFmtSpec
wxT("u "),
186 (count
% 3600)/60) + _("hours");
188 return wxString::Format(
190 wxLongLongFmtSpec
wxT("u %s %02")
191 wxLongLongFmtSpec
wxT("u:%02")
192 wxLongLongFmtSpec
wxT("u "),
195 (count
% 86400)/3600,
196 (count
% 3600)/60) + _("hours");
203 // Examines a filename and determines the filetype
204 FileType
GetFiletype(const CPath
& filename
)
206 // FIXME: WTF do we have two such functions in the first place?
207 switch (GetED2KFileTypeID(filename
)) {
208 case ED2KFT_AUDIO
: return ftAudio
;
209 case ED2KFT_VIDEO
: return ftVideo
;
210 case ED2KFT_IMAGE
: return ftPicture
;
211 case ED2KFT_PROGRAM
: return ftProgram
;
212 case ED2KFT_DOCUMENT
: return ftText
;
213 case ED2KFT_ARCHIVE
: return ftArchive
;
214 case ED2KFT_CDIMAGE
: return ftCDImage
;
215 default: return ftAny
;
220 // Returns the (translated) description assosiated with a FileType
221 wxString
GetFiletypeDesc(FileType type
, bool translated
)
228 return wxT("Videos");
240 return _("Archives");
242 return wxT("Archives");
247 return _("CD-Images");
249 return wxT("CD-Images");
254 return _("Pictures");
256 return wxT("Pictures");
268 return _("Programs");
270 return wxT("Programs");
283 // Returns the Typename, examining the extention of the given filename
285 wxString
GetFiletypeByName(const CPath
& filename
, bool translated
)
287 return GetFiletypeDesc(GetFiletype(filename
), translated
);
291 // Get the max number of connections that the OS supports, or -1 for default
292 int GetMaxConnections()
296 // Try to get the max connection value in the registry
297 wxRegKey
key( wxT("HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\Services\\VxD\\MSTCP\\MaxConnections") );
299 if ( key
.Exists() ) {
300 value
= key
.QueryDefaultValue();
302 if ( !value
.IsEmpty() && value
.IsNumber() ) {
307 switch (wxGetOsVersion()) {
308 case wxOS_WINDOWS_9X
:
309 // This includes all Win9x versions
312 case wxOS_WINDOWS_NT
:
313 // This includes NT based windows
317 // Anything else. Let aMule decide...
322 // Any other OS can just use the default number of connections
328 // Return the text assosiated with a rating of a file
329 wxString
GetRateString(uint16 rate
)
332 case 0: return _("Not rated");
333 case 1: return _("Invalid / Corrupt / Fake");
334 case 2: return _("Poor");
335 case 3: return _("Fair");
336 case 4: return _("Good");
337 case 5: return _("Excellent");
338 default: return _("Not rated");
344 * Return the size in bytes of the given size-type
346 * @param type The type (as an int) where: 0 = Byte, 1 = KB, 2 = MB, 3 = GB
348 * @return The amount of Bytes the provided size-type represents
350 * Values over GB aren't handled since the amount of Bytes 1TB represents
351 * is over the uint32 capacity
353 uint32
GetTypeSize(uint8 type
)
355 enum {Bytes
, KB
, MB
, GB
};
359 case Bytes
: size
= 1; break;
360 case KB
: size
= 1024; break;
361 case MB
: size
= 1048576; break;
362 case GB
: size
= 1073741824; break;
363 default: size
= -1; break;
369 // Base16 chars for encode an decode functions
370 static wxChar base16Chars
[17] = wxT("0123456789ABCDEF");
371 static wxChar base32Chars
[33] = wxT("ABCDEFGHIJKLMNOPQRSTUVWXYZ234567");
372 #define BASE16_LOOKUP_MAX 23
373 static wxChar base16Lookup
[BASE16_LOOKUP_MAX
][2] = {
400 // Returns a BASE16 encoded byte array
403 // buffer: Pointer to byte array
404 // bufLen: Lenght of buffer array
407 // wxString object with BASE16 encoded byte array
408 wxString
EncodeBase16(const unsigned char* buffer
, unsigned int bufLen
)
412 for(unsigned int i
= 0; i
< bufLen
; ++i
) {
413 Base16Buff
+= base16Chars
[buffer
[i
] >> 4];
414 Base16Buff
+= base16Chars
[buffer
[i
] & 0xf];
421 // Decodes a BASE16 string into a byte array
424 // base16Buffer: String containing BASE16
425 // base16BufLen: Lenght BASE16 coded string's length
428 // buffer: byte array containing decoded string
429 unsigned int DecodeBase16(const wxString
&base16Buffer
, unsigned int base16BufLen
, byte
*buffer
)
431 if (base16BufLen
& 1) {
434 unsigned int ret
= base16BufLen
>> 1;
435 memset( buffer
, 0, ret
);
436 for(unsigned int i
= 0; i
< base16BufLen
; ++i
) {
437 int lookup
= toupper(base16Buffer
[i
]) - wxT('0');
438 // Check to make sure that the given word falls inside a valid range
439 byte word
= (lookup
< 0 || lookup
>= BASE16_LOOKUP_MAX
) ?
440 0xFF : base16Lookup
[lookup
][1];
441 unsigned idx
= i
>> 1;
442 buffer
[idx
] = (i
& 1) ? // odd or even?
443 (buffer
[idx
] | word
) : (word
<< 4);
450 // Returns a BASE32 encoded byte array
453 // buffer: Pointer to byte array
454 // bufLen: Lenght of buffer array
457 // wxString object with BASE32 encoded byte array
458 wxString
EncodeBase32(const unsigned char* buffer
, unsigned int bufLen
)
461 unsigned int i
, index
;
464 for(i
= 0, index
= 0; i
< bufLen
;) {
465 // Is the current word going to span a byte boundary?
467 word
= (buffer
[i
] & (0xFF >> index
));
468 index
= (index
+ 5) % 8;
470 if (i
< bufLen
- 1) {
471 word
|= buffer
[i
+ 1] >> (8 - index
);
475 word
= (buffer
[i
] >> (8 - (index
+ 5))) & 0x1F;
476 index
= (index
+ 5) % 8;
481 Base32Buff
+= (char) base32Chars
[word
];
488 // Decodes a BASE32 string into a byte array
491 // base32Buffer: String containing BASE32
492 // base32BufLen: Lenght BASE32 coded string's length
495 // buffer: byte array containing decoded string
498 unsigned int DecodeBase32(const wxString
&base32Buffer
, unsigned int base32BufLen
, unsigned char *buffer
)
500 size_t nInputLen
= base32Buffer
.Length();
501 uint32 nDecodeLen
= (nInputLen
* 5) / 8;
502 if ((nInputLen
* 5) % 8 > 0) {
505 if (base32BufLen
== 0) {
508 if (nDecodeLen
> base32BufLen
) {
514 for (size_t i
= 0; i
< nInputLen
; ++i
)
516 if (base32Buffer
[i
] >= wxT('A') && base32Buffer
[i
] <= wxT('Z')) {
517 nBits
|= ( base32Buffer
[i
] - wxT('A') );
519 else if (base32Buffer
[i
] >= wxT('a') && base32Buffer
[i
] <= wxT('z')) {
520 nBits
|= ( base32Buffer
[i
] - wxT('a') );
522 else if (base32Buffer
[i
] >= wxT('2') && base32Buffer
[i
] <= wxT('7')) {
523 nBits
|= ( base32Buffer
[i
] - wxT('2') + 26 );
530 *buffer
++ = (byte
)( nBits
>> (nCount
- 8) );
543 * Base64 encoding/decoding command line filter
545 * Copyright (c) 2002 Matthias Gaertner 29.06.2002
546 * Adapted by (C) 2005-2008 Phoenix to use wxWidgets.
549 static const wxString
to_b64(
550 /* 0000000000111111111122222222223333333333444444444455555555556666 */
551 /* 0123456789012345678901234567890123456789012345678901234567890123 */
552 wxT("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"));
555 /* Option variables */
556 static bool g_fUseCRLF
= false;
557 static unsigned int g_nCharsPerLine
= 72;
558 static wxString strHeaderLine
;
561 wxString
EncodeBase64(const char *pbBufferIn
, unsigned int bufLen
)
563 wxString pbBufferOut
;
566 if( !strHeaderLine
.IsEmpty() ) {
567 strHeader
= wxT("-----BEGIN ") + strHeaderLine
+ wxT("-----");
569 strHeader
+= wxT("\r");
571 strHeader
+= wxT("\n");
574 unsigned long nDiv
= ((unsigned long)bufLen
) / 3;
575 unsigned long nRem
= ((unsigned long)bufLen
) % 3;
576 unsigned int NewLineSize
= g_fUseCRLF
? 2 : 1;
578 // Allocate enough space in the output buffer to speed up things
580 strHeader
.Len() * 2 + // header/footer
581 (bufLen
* 4) / 3 + 1 + // Number of codes
582 nDiv
* NewLineSize
+ // Number of new lines
583 (nRem
? 1 : 0) * NewLineSize
); // Last line
584 pbBufferOut
= strHeader
;
586 unsigned long nChars
= 0;
587 const unsigned char *pIn
= (unsigned char*)pbBufferIn
;
589 pbBufferOut
+= to_b64
[ (pIn
[0] >> 2) & 0x3f];
590 pbBufferOut
+= to_b64
[((pIn
[0] << 4) & 0x30) | ((pIn
[1] >> 4) & 0xf)];
591 pbBufferOut
+= to_b64
[((pIn
[1] << 2) & 0x3c) | ((pIn
[2] >> 6) & 0x3)];
592 pbBufferOut
+= to_b64
[ pIn
[2] & 0x3f];
596 if( nChars
>= g_nCharsPerLine
&& g_nCharsPerLine
!= 0 ) {
599 pbBufferOut
+= wxT("\r");
601 pbBufferOut
+= wxT("\n");
606 pbBufferOut
+= to_b64
[ (pIn
[0] >> 2) & 0x3f];
607 pbBufferOut
+= to_b64
[((pIn
[0] << 4) & 0x30) | ((pIn
[1] >> 4) & 0xf)];
608 pbBufferOut
+= to_b64
[ (pIn
[1] << 2) & 0x3c];
609 pbBufferOut
+= wxT("=");
611 if( nChars
>= g_nCharsPerLine
&& g_nCharsPerLine
!= 0 ) {
614 pbBufferOut
+= wxT("\r");
616 pbBufferOut
+= wxT("\n");
620 pbBufferOut
+= to_b64
[ (pIn
[0] >> 2) & 0x3f];
621 pbBufferOut
+= to_b64
[ (pIn
[0] << 4) & 0x30];
622 pbBufferOut
+= wxT("=");
623 pbBufferOut
+= wxT("=");
625 if( nChars
>= g_nCharsPerLine
&& g_nCharsPerLine
!= 0 ) {
628 pbBufferOut
+= wxT("\r");
630 pbBufferOut
+= wxT("\n");
638 pbBufferOut
+= wxT("\r");
640 pbBufferOut
+= wxT("\n");
644 if( !strHeaderLine
.IsEmpty() ) {
645 pbBufferOut
= wxT("-----END ") + strHeaderLine
+ wxT("-----");
647 pbBufferOut
+= wxT("\r");
649 pbBufferOut
+= wxT("\n");
656 unsigned int DecodeBase64(const wxString
&base64Buffer
, unsigned int base64BufLen
, unsigned char *buffer
)
658 int z
= 0; // 0 Normal, 1 skip MIME separator (---) to end of line
659 unsigned int nData
= 0;
662 if (base64BufLen
== 0) {
667 for(unsigned int j
= 0; j
< base64BufLen
; ++j
) {
668 wxChar c
= base64Buffer
[j
];
669 wxChar bits
= wxT('z');
675 else if(c
>= wxT('A') && c
<= wxT('Z')) {
678 else if(c
>= wxT('a') && c
<= wxT('z')) {
679 bits
= c
- wxT('a') + (wxChar
)26;
681 else if(c
>= wxT('0') && c
<= wxT('9')) {
682 bits
= c
- wxT('0') + (wxChar
)52;
684 else if(c
== wxT('+')) {
687 else if(c
== wxT('/')) {
690 else if(c
== wxT('-')) {
693 else if(c
== wxT('=')) {
699 // Skips anything that was not recognized
700 // as a base64 valid char ('y' or 'z')
701 if (bits
< (wxChar
)64) {
704 buffer
[i
+0] = (bits
<< 2) & 0xfc;
707 buffer
[i
+0] |= (bits
>> 4) & 0x03;
708 buffer
[i
+1] = (bits
<< 4) & 0xf0;
711 buffer
[i
+1] |= (bits
>> 2) & 0x0f;
712 buffer
[i
+2] = (bits
<< 6) & 0xc0;
715 buffer
[i
+2] |= bits
& 0x3f;
725 // Syntax error or buffer was empty
737 // Returns the text assosiated with a category type
738 wxString
GetCatTitle(int catid
)
741 case 0: return _("all");
742 case 1: return _("all others");
743 case 2: return _("Incomplete");
744 case 3: return _("Completed");
745 case 4: return _("Waiting");
746 case 5: return _("Downloading");
747 case 6: return _("Erroneous");
748 case 7: return _("Paused");
749 case 8: return _("Stopped");
750 case 9: return _("Video");
751 case 10: return _("Audio");
752 case 11: return _("Archive");
753 case 12: return _("CD-Images");
754 case 13: return _("Pictures");
755 case 14: return _("Text");
756 case 15: return _("Active");
757 default: return wxT("?");
762 typedef std::map
<wxString
, EED2KFileTypeClass
> SED2KFileTypeMap
;
763 typedef SED2KFileTypeMap::value_type SED2KFileTypeMapElement
;
764 static SED2KFileTypeMap ED2KFileTypesMap
;
767 class CED2KFileTypes
{
771 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".669"), ED2KFT_AUDIO
));
772 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".aac"), ED2KFT_AUDIO
));
773 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".aif"), ED2KFT_AUDIO
));
774 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".aiff"), ED2KFT_AUDIO
));
775 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".amf"), ED2KFT_AUDIO
));
776 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".ams"), ED2KFT_AUDIO
));
777 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".ape"), ED2KFT_AUDIO
));
778 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".au"), ED2KFT_AUDIO
));
779 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".dbm"), ED2KFT_AUDIO
));
780 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".dmf"), ED2KFT_AUDIO
));
781 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".dsm"), ED2KFT_AUDIO
));
782 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".far"), ED2KFT_AUDIO
));
783 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".flac"), ED2KFT_AUDIO
));
784 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".it"), ED2KFT_AUDIO
));
785 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mdl"), ED2KFT_AUDIO
));
786 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".med"), ED2KFT_AUDIO
));
787 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mid"), ED2KFT_AUDIO
));
788 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".midi"), ED2KFT_AUDIO
));
789 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mod"), ED2KFT_AUDIO
));
790 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mol"), ED2KFT_AUDIO
));
791 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mp1"), ED2KFT_AUDIO
));
792 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mp2"), ED2KFT_AUDIO
));
793 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mp3"), ED2KFT_AUDIO
));
794 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mp4"), ED2KFT_AUDIO
));
795 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mpa"), ED2KFT_AUDIO
));
796 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mpc"), ED2KFT_AUDIO
));
797 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mpp"), ED2KFT_AUDIO
));
798 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mtm"), ED2KFT_AUDIO
));
799 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".nst"), ED2KFT_AUDIO
));
800 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".ogg"), ED2KFT_AUDIO
));
801 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".okt"), ED2KFT_AUDIO
));
802 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".psm"), ED2KFT_AUDIO
));
803 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".ptm"), ED2KFT_AUDIO
));
804 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".ra"), ED2KFT_AUDIO
));
805 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".rmi"), ED2KFT_AUDIO
));
806 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".s3m"), ED2KFT_AUDIO
));
807 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".stm"), ED2KFT_AUDIO
));
808 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".ult"), ED2KFT_AUDIO
));
809 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".umx"), ED2KFT_AUDIO
));
810 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".wav"), ED2KFT_AUDIO
));
811 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".wma"), ED2KFT_AUDIO
));
812 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".wow"), ED2KFT_AUDIO
));
813 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".xm"), ED2KFT_AUDIO
));
815 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".asf"), ED2KFT_VIDEO
));
816 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".avi"), ED2KFT_VIDEO
));
817 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".divx"), ED2KFT_VIDEO
));
818 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".m1v"), ED2KFT_VIDEO
));
819 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".m2v"), ED2KFT_VIDEO
));
820 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mkv"), ED2KFT_VIDEO
));
821 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mov"), ED2KFT_VIDEO
));
822 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mp1v"), ED2KFT_VIDEO
));
823 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mp2v"), ED2KFT_VIDEO
));
824 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mpe"), ED2KFT_VIDEO
));
825 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mpeg"), ED2KFT_VIDEO
));
826 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mpg"), ED2KFT_VIDEO
));
827 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mps"), ED2KFT_VIDEO
));
828 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mpv"), ED2KFT_VIDEO
));
829 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mpv1"), ED2KFT_VIDEO
));
830 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mpv2"), ED2KFT_VIDEO
));
831 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".ogm"), ED2KFT_VIDEO
));
832 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".qt"), ED2KFT_VIDEO
));
833 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".ram"), ED2KFT_VIDEO
));
834 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".rm"), ED2KFT_VIDEO
));
835 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".rv"), ED2KFT_VIDEO
));
836 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".rv9"), ED2KFT_VIDEO
));
837 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".ts"), ED2KFT_VIDEO
));
838 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".vivo"), ED2KFT_VIDEO
));
839 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".vob"), ED2KFT_VIDEO
));
840 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".wmv"), ED2KFT_VIDEO
));
841 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".xvid"), ED2KFT_VIDEO
));
843 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".bmp"), ED2KFT_IMAGE
));
844 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".dcx"), ED2KFT_IMAGE
));
845 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".emf"), ED2KFT_IMAGE
));
846 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".gif"), ED2KFT_IMAGE
));
847 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".ico"), ED2KFT_IMAGE
));
848 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".jpeg"), ED2KFT_IMAGE
));
849 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".jpg"), ED2KFT_IMAGE
));
850 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".pct"), ED2KFT_IMAGE
));
851 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".pcx"), ED2KFT_IMAGE
));
852 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".pic"), ED2KFT_IMAGE
));
853 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".pict"), ED2KFT_IMAGE
));
854 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".png"), ED2KFT_IMAGE
));
855 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".psd"), ED2KFT_IMAGE
));
856 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".psp"), ED2KFT_IMAGE
));
857 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".tga"), ED2KFT_IMAGE
));
858 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".tif"), ED2KFT_IMAGE
));
859 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".tiff"), ED2KFT_IMAGE
));
860 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".wmf"), ED2KFT_IMAGE
));
861 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".xif"), ED2KFT_IMAGE
));
863 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".7z"), ED2KFT_ARCHIVE
));
864 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".ace"), ED2KFT_ARCHIVE
));
865 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".arj"), ED2KFT_ARCHIVE
));
866 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".bz2"), ED2KFT_ARCHIVE
));
867 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".cab"), ED2KFT_ARCHIVE
));
868 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".gz"), ED2KFT_ARCHIVE
));
869 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".hqx"), ED2KFT_ARCHIVE
));
870 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".lha"), ED2KFT_ARCHIVE
));
871 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".msi"), ED2KFT_ARCHIVE
));
872 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".rar"), ED2KFT_ARCHIVE
));
873 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".sea"), ED2KFT_ARCHIVE
));
874 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".sit"), ED2KFT_ARCHIVE
));
875 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".tar"), ED2KFT_ARCHIVE
));
876 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".tgz"), ED2KFT_ARCHIVE
));
877 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".uc2"), ED2KFT_ARCHIVE
));
878 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".zip"), ED2KFT_ARCHIVE
));
880 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".bat"), ED2KFT_PROGRAM
));
881 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".cmd"), ED2KFT_PROGRAM
));
882 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".com"), ED2KFT_PROGRAM
));
883 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".exe"), ED2KFT_PROGRAM
));
885 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".bin"), ED2KFT_CDIMAGE
));
886 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".bwa"), ED2KFT_CDIMAGE
));
887 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".bwi"), ED2KFT_CDIMAGE
));
888 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".bws"), ED2KFT_CDIMAGE
));
889 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".bwt"), ED2KFT_CDIMAGE
));
890 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".ccd"), ED2KFT_CDIMAGE
));
891 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".cue"), ED2KFT_CDIMAGE
));
892 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".dmg"), ED2KFT_CDIMAGE
));
893 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".dmz"), ED2KFT_CDIMAGE
));
894 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".img"), ED2KFT_CDIMAGE
));
895 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".iso"), ED2KFT_CDIMAGE
));
896 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mdf"), ED2KFT_CDIMAGE
));
897 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mds"), ED2KFT_CDIMAGE
));
898 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".nrg"), ED2KFT_CDIMAGE
));
899 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".sub"), ED2KFT_CDIMAGE
));
900 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".toast"), ED2KFT_CDIMAGE
));
902 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".chm"), ED2KFT_DOCUMENT
));
903 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".css"), ED2KFT_DOCUMENT
));
904 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".diz"), ED2KFT_DOCUMENT
));
905 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".doc"), ED2KFT_DOCUMENT
));
906 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".dot"), ED2KFT_DOCUMENT
));
907 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".hlp"), ED2KFT_DOCUMENT
));
908 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".htm"), ED2KFT_DOCUMENT
));
909 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".html"), ED2KFT_DOCUMENT
));
910 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".nfo"), ED2KFT_DOCUMENT
));
911 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".pdf"), ED2KFT_DOCUMENT
));
912 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".pps"), ED2KFT_DOCUMENT
));
913 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".ppt"), ED2KFT_DOCUMENT
));
914 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".ps"), ED2KFT_DOCUMENT
));
915 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".rtf"), ED2KFT_DOCUMENT
));
916 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".wri"), ED2KFT_DOCUMENT
));
917 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".txt"), ED2KFT_DOCUMENT
));
918 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".xls"), ED2KFT_DOCUMENT
));
919 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".xlt"), ED2KFT_DOCUMENT
));
924 // get the list initialized *before* any code is accessing it
925 CED2KFileTypes theED2KFileTypes
;
927 EED2KFileType
GetED2KFileTypeID(const CPath
& fileName
)
929 const wxString ext
= fileName
.GetExt().Lower();
934 SED2KFileTypeMap::iterator it
= ED2KFileTypesMap
.find(wxT(".") + ext
);
935 if (it
!= ED2KFileTypesMap
.end()) {
936 return it
->second
.GetType();
943 // Retuns the ed2k file type term which is to be used in server searches
944 wxString
GetED2KFileTypeSearchTerm(EED2KFileType iFileID
)
946 if (iFileID
== ED2KFT_AUDIO
) return ED2KFTSTR_AUDIO
;
947 if (iFileID
== ED2KFT_VIDEO
) return ED2KFTSTR_VIDEO
;
948 if (iFileID
== ED2KFT_IMAGE
) return ED2KFTSTR_IMAGE
;
949 if (iFileID
== ED2KFT_DOCUMENT
) return ED2KFTSTR_DOCUMENT
;
950 if (iFileID
== ED2KFT_PROGRAM
) return ED2KFTSTR_PROGRAM
;
951 // NOTE: Archives and CD-Images are published with file type "Pro"
952 if (iFileID
== ED2KFT_ARCHIVE
) return ED2KFTSTR_PROGRAM
;
953 if (iFileID
== ED2KFT_CDIMAGE
) return ED2KFTSTR_PROGRAM
;
955 return wxEmptyString
;
959 // Returns a file type which is used eMule internally only, examining the extention of the given filename
960 wxString
GetFileTypeByName(const CPath
& fileName
)
962 EED2KFileType iFileType
= GetED2KFileTypeID(fileName
);
964 case ED2KFT_AUDIO
: return ED2KFTSTR_AUDIO
;
965 case ED2KFT_VIDEO
: return ED2KFTSTR_VIDEO
;
966 case ED2KFT_IMAGE
: return ED2KFTSTR_IMAGE
;
967 case ED2KFT_DOCUMENT
: return ED2KFTSTR_DOCUMENT
;
968 case ED2KFT_PROGRAM
: return ED2KFTSTR_PROGRAM
;
969 case ED2KFT_ARCHIVE
: return ED2KFTSTR_ARCHIVE
;
970 case ED2KFT_CDIMAGE
: return ED2KFTSTR_CDIMAGE
;
971 default: return wxEmptyString
;
976 // Retuns the ed2k file type integer ID which is to be used for publishing+searching
977 EED2KFileType
GetED2KFileTypeSearchID(EED2KFileType iFileID
)
980 case ED2KFT_AUDIO
: return ED2KFT_AUDIO
;
981 case ED2KFT_VIDEO
: return ED2KFT_VIDEO
;
982 case ED2KFT_IMAGE
: return ED2KFT_IMAGE
;
983 case ED2KFT_DOCUMENT
: return ED2KFT_DOCUMENT
;
984 case ED2KFT_PROGRAM
: return ED2KFT_PROGRAM
;
985 // NOTE: Archives and CD-Images are published+searched with file type "Pro"
986 // NOTE: If this gets changed, the function 'GetED2KFileTypeSearchTerm' also needs to get updated!
987 case ED2KFT_ARCHIVE
: return ED2KFT_PROGRAM
;
988 case ED2KFT_CDIMAGE
: return ED2KFT_PROGRAM
;
989 default: return ED2KFT_ANY
;
995 * Dumps a buffer to a wxString
997 wxString
DumpMemToStr(const void *buff
, int n
, const wxString
& msg
, bool ok
)
999 const unsigned char *p
= (const unsigned char *)buff
;
1000 int lines
= (n
+ 15)/ 16;
1003 // Allocate aproximetly what is needed
1004 result
.Alloc( ( lines
+ 1 ) * 80 );
1005 if ( !msg
.IsEmpty() ) {
1006 result
+= msg
+ wxT(" - ok=") + ( ok
? wxT("true, ") : wxT("false, ") );
1009 result
+= wxString::Format( wxT("%d bytes\n"), n
);
1010 for ( int i
= 0; i
< lines
; ++i
) {
1012 result
+= wxString::Format( wxT("%08x "), i
* 16 );
1014 // Show two columns of hex-values
1015 for ( int j
= 0; j
< 2; ++j
) {
1016 for ( int k
= 0; k
< 8; ++k
) {
1017 int pos
= 16 * i
+ 8 * j
+ k
;
1020 result
+= wxString::Format( wxT("%02x "), p
[pos
] );
1028 // Show a column of ascii-values
1029 for ( int k
= 0; k
< 16; ++k
) {
1030 int pos
= 16 * i
+ k
;
1033 if ( isspace( p
[pos
] ) ) {
1035 } else if ( !isgraph( p
[pos
] ) ) {
1038 result
+= (wxChar
)p
[pos
];
1044 result
+= wxT("|\n");
1053 * Dumps a buffer to stdout
1055 void DumpMem(const void *buff
, int n
, const wxString
& msg
, bool ok
)
1057 printf("%s\n", (const char*)unicode2char(DumpMemToStr( buff
, n
, msg
, ok
)) );
1062 // Dump mem in dword format
1063 void DumpMem_DW(const uint32
*ptr
, int count
)
1065 for(int i
= 0; i
< count
; i
++) {
1066 printf("%08x ", ptr
[i
]);
1067 if ( (i
% 4) == 3) printf("\n");
1073 void MilliSleep(uint32 msecs
)
1081 struct timespec waittime
;
1082 waittime
.tv_sec
= 0;
1083 waittime
.tv_nsec
= msecs
* 1000 /*micro*/* 1000 /*nano*/;
1084 struct timespec remtime
;
1085 while ((nanosleep(&waittime
,&remtime
)==-1) && (errno
== EINTR
)) {
1086 memcpy(&waittime
,&remtime
,sizeof(struct timespec
));
1090 wxMilliSleep(msecs
);
1095 wxString
GetConfigDir()
1098 static wxString configPath
;
1100 if (configPath
.IsEmpty()) {
1102 // "Portable aMule" - Use aMule from an external USB drive
1103 // Check for ./config/amule.conf and use this configuration if found
1104 const wxString configDir
= JoinPaths(wxFileName::GetCwd(), wxT("config"));
1105 const wxString configFile
= JoinPaths(configDir
, wxT("amule.conf"));
1107 if (CPath::DirExists(configDir
) && CPath::FileExists(configFile
)) {
1108 AddLogLineM(true, CFormat(wxT("Using configDir: %s")) % configDir
);
1110 configPath
= configDir
;
1112 configPath
= wxStandardPaths::Get().GetUserDataDir();
1115 configPath
= wxStandardPaths::Get().GetUserDataDir();
1118 configPath
+= wxFileName::GetPathSeparator();
1125 void InitCustomLanguages()
1127 wxLanguageInfo CustomLanguage
;
1128 CustomLanguage
.Language
= wxLANGUAGE_ITALIAN_NAPOLITAN
;
1129 CustomLanguage
.CanonicalName
= wxT("it_NA");
1130 CustomLanguage
.Description
= wxT("sNeo's Custom Napolitan Language");
1131 wxLocale::AddLanguage(CustomLanguage
);
1135 void InitLocale(wxLocale
& locale
, int language
)
1137 int language_flags
= 0;
1138 if (language
!= wxLANGUAGE_CUSTOM
&& language
!= wxLANGUAGE_ITALIAN_NAPOLITAN
) {
1139 language_flags
= wxLOCALE_LOAD_DEFAULT
| wxLOCALE_CONV_ENCODING
;
1142 locale
.Init(language
,language_flags
);
1144 if (language
!= wxLANGUAGE_CUSTOM
) {
1146 #if defined(__WXMAC__) || defined(__WXMSW__)
1147 locale
.AddCatalogLookupPathPrefix(JoinPaths(wxStandardPaths::Get().GetDataDir(), wxT("locale")));
1149 locale
.AddCatalog(wxT(PACKAGE
));
1152 locale
.AddCatalogLookupPathPrefix(GetConfigDir());
1153 locale
.AddCatalog(wxT("custom"));
1158 int StrLang2wx(const wxString
& language
)
1160 // get rid of possible encoding and modifier
1161 wxString
lang(language
.BeforeFirst('.').BeforeFirst('@'));
1163 if (!lang
.IsEmpty()) {
1164 const wxLanguageInfo
*lng
= wxLocale::FindLanguageInfo(lang
);
1166 return lng
->Language
;
1168 return wxLANGUAGE_DEFAULT
;
1171 return wxLANGUAGE_DEFAULT
;
1176 wxString
wxLang2Str(const int lang
)
1178 if (lang
!= wxLANGUAGE_DEFAULT
) {
1179 const wxLanguageInfo
*lng
= wxLocale::GetLanguageInfo(lang
);
1181 return lng
->CanonicalName
;
1183 return wxEmptyString
;
1186 return wxEmptyString
;
1190 wxString
GetPassword() {
1191 wxString pass_plain
;
1194 pass_plain
= char2unicode(getpass("Enter password for mule connection: "));
1196 //#warning This way, pass enter is not hidden on windows. Bad thing.
1199 printf("Enter password for mule connection: \n");
1201 fgets(temp_str
, 512, stdin
);
1202 temp_str
[strlen(temp_str
)-1] = '\0';
1203 pass_plain
= char2unicode(temp_str
);
1205 wxCHECK2(password
.Decode(MD5Sum(pass_plain
).GetHash()), /* Do nothing. */ );
1206 // MD5 hash for an empty string, according to rfc1321.
1207 if (password
.Encode() == wxT("D41D8CD98F00B204E9800998ECF8427E")) {
1208 printf("No empty password allowed.\n");
1209 return GetPassword();
1213 return password
.Encode();
1216 // File_checked_for_headers