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
61 #if !defined(AMULE_DAEMON) && (!defined(EC_REMOTE) || defined(CLIENT_GUI))
62 #include "amule.h" // Needed for theApp
66 wxString
GetMuleVersion()
68 wxString
ver(wxT(VERSION
));
70 ver
+= wxT(" using ");
73 // Figure out the wx build-type
80 #elif defined(__WXMAC__)
82 #elif defined(__WXMSW__)
84 #elif defined(__WXCOCOA__)
85 ver
+= wxT("wxCocoa");
88 ver
+= wxString::Format(wxT(" v%d.%d.%d"), wxMAJOR_VERSION
, wxMINOR_VERSION
, wxRELEASE_NUMBER
);
90 #if defined(__WXDEBUG__)
91 ver
+= wxT(" (Debugging)");
95 ver
+= wxString::Format( wxT(" (Snapshot: %s)"), wxT(SVNDATE
));
102 wxString
GetFullMuleVersion()
105 wxString app
= wxT("aMuled");
106 #elif defined(CLIENT_GUI)
107 wxString app
= wxT("Remote aMule-GUI");
109 wxString app
= wxT("aMule");
112 return app
+ wxT(" ") + GetMuleVersion();
116 // Formats a filesize in bytes to make it suitable for displaying
117 wxString
CastItoXBytes( uint64 count
)
121 return wxString::Format( wxT("%.0f "), (float)(uint32
)count
) + wxPLURAL("byte", "bytes", count
) ;
122 else if (count
< 1048576)
123 return wxString::Format( wxT("%.0f "), (float)(uint32
)count
/1024) + _("kB") ;
124 else if (count
< 1073741824)
125 return wxString::Format( wxT("%.2f "), (float)(uint32
)count
/1048576) + _("MB") ;
126 else if (count
< 1099511627776LL)
127 return wxString::Format( wxT("%.2f "), (float)((uint32
)(count
/1024))/1048576) + _("GB") ;
129 return wxString::Format( wxT("%.3f "), (float)count
/1099511627776LL) + _("TB") ;
135 wxString
CastItoIShort(uint64 count
)
139 return wxString::Format(wxT("%u"), (uint32
)count
);
140 else if (count
< 1000000)
141 return wxString::Format(wxT("%.0f"),(float)(uint32
)count
/1000) + _("k") ;
142 else if (count
< 1000000000)
143 return wxString::Format(wxT("%.2f"),(float)(uint32
)count
/1000000) + _("M") ;
144 else if (count
< 1000000000000LL)
145 return wxString::Format(wxT("%.2f"),(float)((uint32
)(count
/1000))/1000000) + _("G") ;
146 else if (count
< 1000000000000000LL)
147 return wxString::Format(wxT("%.2f"),(float)count
/1000000000000LL) + _("T");
153 wxString
CastItoSpeed(uint32 bytes
)
156 return wxString::Format(wxT("%u "), bytes
) + wxPLURAL("byte/sec", "bytes/sec", bytes
);
157 else if (bytes
< 1048576)
158 return wxString::Format(wxT("%.2f "), bytes
/ 1024.0) + _("kB/s");
160 return wxString::Format(wxT("%.2f "), bytes
/ 1048576.0) + _("MB/s");
164 // Make a time value in seconds suitable for displaying
165 wxString
CastSecondsToHM(uint64 count
, uint16 msecs
)
169 return wxString::Format(
170 wxT("%02") wxLongLongFmtSpec
wxT("u "),
173 return wxString::Format(
175 (count
+ ((float)msecs
/1000))) + _("secs");
177 } else if (count
< 3600) {
178 return wxString::Format(
180 wxLongLongFmtSpec
wxT("u:%02")
181 wxLongLongFmtSpec
wxT("u "),
183 (count
% 60)) + _("mins");
184 } else if (count
< 86400) {
185 return wxString::Format(
187 wxLongLongFmtSpec
wxT("u:%02")
188 wxLongLongFmtSpec
wxT("u "),
190 (count
% 3600)/60) + _("hours");
192 return wxString::Format(
194 wxLongLongFmtSpec
wxT("u %s %02")
195 wxLongLongFmtSpec
wxT("u:%02")
196 wxLongLongFmtSpec
wxT("u "),
199 (count
% 86400)/3600,
200 (count
% 3600)/60) + _("hours");
207 // Examines a filename and determines the filetype
208 FileType
GetFiletype(const CPath
& filename
)
210 // FIXME: WTF do we have two such functions in the first place?
211 switch (GetED2KFileTypeID(filename
)) {
212 case ED2KFT_AUDIO
: return ftAudio
;
213 case ED2KFT_VIDEO
: return ftVideo
;
214 case ED2KFT_IMAGE
: return ftPicture
;
215 case ED2KFT_PROGRAM
: return ftProgram
;
216 case ED2KFT_DOCUMENT
: return ftText
;
217 case ED2KFT_ARCHIVE
: return ftArchive
;
218 case ED2KFT_CDIMAGE
: return ftCDImage
;
219 default: return ftAny
;
224 // Returns the (translated) description assosiated with a FileType
225 wxString
GetFiletypeDesc(FileType type
, bool translated
)
232 return wxT("Videos");
244 return _("Archives");
246 return wxT("Archives");
251 return _("CD-Images");
253 return wxT("CD-Images");
258 return _("Pictures");
260 return wxT("Pictures");
272 return _("Programs");
274 return wxT("Programs");
287 // Returns the Typename, examining the extention of the given filename
289 wxString
GetFiletypeByName(const CPath
& filename
, bool translated
)
291 return GetFiletypeDesc(GetFiletype(filename
), translated
);
295 // Get the max number of connections that the OS supports, or -1 for default
296 int GetMaxConnections()
300 // Try to get the max connection value in the registry
301 wxRegKey
key( wxT("HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\Services\\VxD\\MSTCP\\MaxConnections") );
303 if ( key
.Exists() ) {
304 value
= key
.QueryDefaultValue();
306 if ( !value
.IsEmpty() && value
.IsNumber() ) {
311 switch (wxGetOsVersion()) {
312 case wxOS_WINDOWS_9X
:
313 // This includes all Win9x versions
316 case wxOS_WINDOWS_NT
:
317 // This includes NT based windows
321 // Anything else. Let aMule decide...
326 // Any other OS can just use the default number of connections
332 // Return the text assosiated with a rating of a file
333 wxString
GetRateString(uint16 rate
)
336 case 0: return _("Not rated");
337 case 1: return _("Invalid / Corrupt / Fake");
338 case 2: return _("Poor");
339 case 3: return _("Fair");
340 case 4: return _("Good");
341 case 5: return _("Excellent");
342 default: return _("Not rated");
348 * Return the size in bytes of the given size-type
350 * @param type The type (as an int) where: 0 = Byte, 1 = KB, 2 = MB, 3 = GB
352 * @return The amount of Bytes the provided size-type represents
354 * Values over GB aren't handled since the amount of Bytes 1TB represents
355 * is over the uint32 capacity
357 uint32
GetTypeSize(uint8 type
)
359 enum {Bytes
, KB
, MB
, GB
};
363 case Bytes
: size
= 1; break;
364 case KB
: size
= 1024; break;
365 case MB
: size
= 1048576; break;
366 case GB
: size
= 1073741824; break;
367 default: size
= -1; break;
373 // Base16 chars for encode an decode functions
374 static wxChar base16Chars
[17] = wxT("0123456789ABCDEF");
375 static wxChar base32Chars
[33] = wxT("ABCDEFGHIJKLMNOPQRSTUVWXYZ234567");
376 #define BASE16_LOOKUP_MAX 23
377 static wxChar base16Lookup
[BASE16_LOOKUP_MAX
][2] = {
404 // Returns a BASE16 encoded byte array
407 // buffer: Pointer to byte array
408 // bufLen: Lenght of buffer array
411 // wxString object with BASE16 encoded byte array
412 wxString
EncodeBase16(const unsigned char* buffer
, unsigned int bufLen
)
416 for(unsigned int i
= 0; i
< bufLen
; ++i
) {
417 Base16Buff
+= base16Chars
[buffer
[i
] >> 4];
418 Base16Buff
+= base16Chars
[buffer
[i
] & 0xf];
425 // Decodes a BASE16 string into a byte array
428 // base16Buffer: String containing BASE16
429 // base16BufLen: Lenght BASE16 coded string's length
432 // buffer: byte array containing decoded string
433 unsigned int DecodeBase16(const wxString
&base16Buffer
, unsigned int base16BufLen
, byte
*buffer
)
435 if (base16BufLen
& 1) {
438 unsigned int ret
= base16BufLen
>> 1;
439 memset( buffer
, 0, ret
);
440 for(unsigned int i
= 0; i
< base16BufLen
; ++i
) {
441 int lookup
= toupper(base16Buffer
[i
]) - wxT('0');
442 // Check to make sure that the given word falls inside a valid range
443 byte word
= (lookup
< 0 || lookup
>= BASE16_LOOKUP_MAX
) ?
444 0xFF : base16Lookup
[lookup
][1];
445 unsigned idx
= i
>> 1;
446 buffer
[idx
] = (i
& 1) ? // odd or even?
447 (buffer
[idx
] | word
) : (word
<< 4);
454 // Returns a BASE32 encoded byte array
457 // buffer: Pointer to byte array
458 // bufLen: Lenght of buffer array
461 // wxString object with BASE32 encoded byte array
462 wxString
EncodeBase32(const unsigned char* buffer
, unsigned int bufLen
)
465 unsigned int i
, index
;
468 for(i
= 0, index
= 0; i
< bufLen
;) {
469 // Is the current word going to span a byte boundary?
471 word
= (buffer
[i
] & (0xFF >> index
));
472 index
= (index
+ 5) % 8;
474 if (i
< bufLen
- 1) {
475 word
|= buffer
[i
+ 1] >> (8 - index
);
479 word
= (buffer
[i
] >> (8 - (index
+ 5))) & 0x1F;
480 index
= (index
+ 5) % 8;
485 Base32Buff
+= (char) base32Chars
[word
];
492 // Decodes a BASE32 string into a byte array
495 // base32Buffer: String containing BASE32
496 // base32BufLen: Lenght BASE32 coded string's length
499 // buffer: byte array containing decoded string
502 unsigned int DecodeBase32(const wxString
&base32Buffer
, unsigned int base32BufLen
, unsigned char *buffer
)
504 size_t nInputLen
= base32Buffer
.Length();
505 uint32 nDecodeLen
= (nInputLen
* 5) / 8;
506 if ((nInputLen
* 5) % 8 > 0) {
509 if (base32BufLen
== 0) {
512 if (nDecodeLen
> base32BufLen
) {
518 for (size_t i
= 0; i
< nInputLen
; ++i
)
520 if (base32Buffer
[i
] >= wxT('A') && base32Buffer
[i
] <= wxT('Z')) {
521 nBits
|= ( base32Buffer
[i
] - wxT('A') );
523 else if (base32Buffer
[i
] >= wxT('a') && base32Buffer
[i
] <= wxT('z')) {
524 nBits
|= ( base32Buffer
[i
] - wxT('a') );
526 else if (base32Buffer
[i
] >= wxT('2') && base32Buffer
[i
] <= wxT('7')) {
527 nBits
|= ( base32Buffer
[i
] - wxT('2') + 26 );
534 *buffer
++ = (byte
)( nBits
>> (nCount
- 8) );
547 * Base64 encoding/decoding command line filter
549 * Copyright (c) 2002 Matthias Gaertner 29.06.2002
550 * Adapted by (C) 2005-2008 Phoenix to use wxWidgets.
553 static const wxString
to_b64(
554 /* 0000000000111111111122222222223333333333444444444455555555556666 */
555 /* 0123456789012345678901234567890123456789012345678901234567890123 */
556 wxT("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"));
559 /* Option variables */
560 static bool g_fUseCRLF
= false;
561 static unsigned int g_nCharsPerLine
= 72;
562 static wxString strHeaderLine
;
565 wxString
EncodeBase64(const char *pbBufferIn
, unsigned int bufLen
)
567 wxString pbBufferOut
;
570 if( !strHeaderLine
.IsEmpty() ) {
571 strHeader
= wxT("-----BEGIN ") + strHeaderLine
+ wxT("-----");
573 strHeader
+= wxT("\r");
575 strHeader
+= wxT("\n");
578 unsigned long nDiv
= ((unsigned long)bufLen
) / 3;
579 unsigned long nRem
= ((unsigned long)bufLen
) % 3;
580 unsigned int NewLineSize
= g_fUseCRLF
? 2 : 1;
582 // Allocate enough space in the output buffer to speed up things
584 strHeader
.Len() * 2 + // header/footer
585 (bufLen
* 4) / 3 + 1 + // Number of codes
586 nDiv
* NewLineSize
+ // Number of new lines
587 (nRem
? 1 : 0) * NewLineSize
); // Last line
588 pbBufferOut
= strHeader
;
590 unsigned long nChars
= 0;
591 const unsigned char *pIn
= (unsigned char*)pbBufferIn
;
593 pbBufferOut
+= to_b64
[ (pIn
[0] >> 2) & 0x3f];
594 pbBufferOut
+= to_b64
[((pIn
[0] << 4) & 0x30) | ((pIn
[1] >> 4) & 0xf)];
595 pbBufferOut
+= to_b64
[((pIn
[1] << 2) & 0x3c) | ((pIn
[2] >> 6) & 0x3)];
596 pbBufferOut
+= to_b64
[ pIn
[2] & 0x3f];
600 if( nChars
>= g_nCharsPerLine
&& g_nCharsPerLine
!= 0 ) {
603 pbBufferOut
+= wxT("\r");
605 pbBufferOut
+= wxT("\n");
610 pbBufferOut
+= to_b64
[ (pIn
[0] >> 2) & 0x3f];
611 pbBufferOut
+= to_b64
[((pIn
[0] << 4) & 0x30) | ((pIn
[1] >> 4) & 0xf)];
612 pbBufferOut
+= to_b64
[ (pIn
[1] << 2) & 0x3c];
613 pbBufferOut
+= wxT("=");
615 if( nChars
>= g_nCharsPerLine
&& g_nCharsPerLine
!= 0 ) {
618 pbBufferOut
+= wxT("\r");
620 pbBufferOut
+= wxT("\n");
624 pbBufferOut
+= to_b64
[ (pIn
[0] >> 2) & 0x3f];
625 pbBufferOut
+= to_b64
[ (pIn
[0] << 4) & 0x30];
626 pbBufferOut
+= wxT("=");
627 pbBufferOut
+= wxT("=");
629 if( nChars
>= g_nCharsPerLine
&& g_nCharsPerLine
!= 0 ) {
632 pbBufferOut
+= wxT("\r");
634 pbBufferOut
+= wxT("\n");
642 pbBufferOut
+= wxT("\r");
644 pbBufferOut
+= wxT("\n");
648 if( !strHeaderLine
.IsEmpty() ) {
649 pbBufferOut
= wxT("-----END ") + strHeaderLine
+ wxT("-----");
651 pbBufferOut
+= wxT("\r");
653 pbBufferOut
+= wxT("\n");
660 unsigned int DecodeBase64(const wxString
&base64Buffer
, unsigned int base64BufLen
, unsigned char *buffer
)
662 int z
= 0; // 0 Normal, 1 skip MIME separator (---) to end of line
663 unsigned int nData
= 0;
666 if (base64BufLen
== 0) {
671 for(unsigned int j
= 0; j
< base64BufLen
; ++j
) {
672 wxChar c
= base64Buffer
[j
];
673 wxChar bits
= wxT('z');
679 else if(c
>= wxT('A') && c
<= wxT('Z')) {
682 else if(c
>= wxT('a') && c
<= wxT('z')) {
683 bits
= c
- wxT('a') + (wxChar
)26;
685 else if(c
>= wxT('0') && c
<= wxT('9')) {
686 bits
= c
- wxT('0') + (wxChar
)52;
688 else if(c
== wxT('+')) {
691 else if(c
== wxT('/')) {
694 else if(c
== wxT('-')) {
697 else if(c
== wxT('=')) {
703 // Skips anything that was not recognized
704 // as a base64 valid char ('y' or 'z')
705 if (bits
< (wxChar
)64) {
708 buffer
[i
+0] = (bits
<< 2) & 0xfc;
711 buffer
[i
+0] |= (bits
>> 4) & 0x03;
712 buffer
[i
+1] = (bits
<< 4) & 0xf0;
715 buffer
[i
+1] |= (bits
>> 2) & 0x0f;
716 buffer
[i
+2] = (bits
<< 6) & 0xc0;
719 buffer
[i
+2] |= bits
& 0x3f;
729 // Syntax error or buffer was empty
741 // Returns the text assosiated with a category type
742 wxString
GetCatTitle(int catid
)
745 case 0: return _("all");
746 case 1: return _("all others");
747 case 2: return _("Incomplete");
748 case 3: return _("Completed");
749 case 4: return _("Waiting");
750 case 5: return _("Downloading");
751 case 6: return _("Erroneous");
752 case 7: return _("Paused");
753 case 8: return _("Stopped");
754 case 9: return _("Video");
755 case 10: return _("Audio");
756 case 11: return _("Archive");
757 case 12: return _("CD-Images");
758 case 13: return _("Pictures");
759 case 14: return _("Text");
760 case 15: return _("Active");
761 default: return wxT("?");
766 typedef std::map
<wxString
, EED2KFileTypeClass
> SED2KFileTypeMap
;
767 typedef SED2KFileTypeMap::value_type SED2KFileTypeMapElement
;
768 static SED2KFileTypeMap ED2KFileTypesMap
;
771 class CED2KFileTypes
{
775 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".669"), ED2KFT_AUDIO
));
776 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".aac"), ED2KFT_AUDIO
));
777 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".aif"), ED2KFT_AUDIO
));
778 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".aiff"), ED2KFT_AUDIO
));
779 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".amf"), ED2KFT_AUDIO
));
780 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".ams"), ED2KFT_AUDIO
));
781 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".ape"), ED2KFT_AUDIO
));
782 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".au"), ED2KFT_AUDIO
));
783 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".dbm"), ED2KFT_AUDIO
));
784 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".dmf"), ED2KFT_AUDIO
));
785 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".dsm"), ED2KFT_AUDIO
));
786 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".far"), ED2KFT_AUDIO
));
787 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".flac"), ED2KFT_AUDIO
));
788 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".it"), ED2KFT_AUDIO
));
789 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mdl"), ED2KFT_AUDIO
));
790 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".med"), ED2KFT_AUDIO
));
791 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mid"), ED2KFT_AUDIO
));
792 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".midi"), ED2KFT_AUDIO
));
793 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mod"), ED2KFT_AUDIO
));
794 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mol"), ED2KFT_AUDIO
));
795 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mp1"), ED2KFT_AUDIO
));
796 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mp2"), ED2KFT_AUDIO
));
797 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mp3"), ED2KFT_AUDIO
));
798 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mp4"), ED2KFT_AUDIO
));
799 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mpa"), ED2KFT_AUDIO
));
800 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mpc"), ED2KFT_AUDIO
));
801 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mpp"), ED2KFT_AUDIO
));
802 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mtm"), ED2KFT_AUDIO
));
803 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".nst"), ED2KFT_AUDIO
));
804 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".ogg"), ED2KFT_AUDIO
));
805 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".okt"), ED2KFT_AUDIO
));
806 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".psm"), ED2KFT_AUDIO
));
807 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".ptm"), ED2KFT_AUDIO
));
808 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".ra"), ED2KFT_AUDIO
));
809 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".rmi"), ED2KFT_AUDIO
));
810 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".s3m"), ED2KFT_AUDIO
));
811 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".stm"), ED2KFT_AUDIO
));
812 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".ult"), ED2KFT_AUDIO
));
813 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".umx"), ED2KFT_AUDIO
));
814 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".wav"), ED2KFT_AUDIO
));
815 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".wma"), ED2KFT_AUDIO
));
816 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".wow"), ED2KFT_AUDIO
));
817 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".xm"), ED2KFT_AUDIO
));
819 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".asf"), ED2KFT_VIDEO
));
820 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".avi"), ED2KFT_VIDEO
));
821 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".divx"), ED2KFT_VIDEO
));
822 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".m1v"), ED2KFT_VIDEO
));
823 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".m2v"), ED2KFT_VIDEO
));
824 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mkv"), ED2KFT_VIDEO
));
825 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mov"), ED2KFT_VIDEO
));
826 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mp1v"), ED2KFT_VIDEO
));
827 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mp2v"), ED2KFT_VIDEO
));
828 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mpe"), ED2KFT_VIDEO
));
829 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mpeg"), ED2KFT_VIDEO
));
830 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mpg"), ED2KFT_VIDEO
));
831 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mps"), ED2KFT_VIDEO
));
832 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mpv"), ED2KFT_VIDEO
));
833 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mpv1"), ED2KFT_VIDEO
));
834 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mpv2"), ED2KFT_VIDEO
));
835 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".ogm"), ED2KFT_VIDEO
));
836 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".qt"), ED2KFT_VIDEO
));
837 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".ram"), ED2KFT_VIDEO
));
838 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".rm"), ED2KFT_VIDEO
));
839 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".rv"), ED2KFT_VIDEO
));
840 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".rv9"), ED2KFT_VIDEO
));
841 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".ts"), ED2KFT_VIDEO
));
842 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".vivo"), ED2KFT_VIDEO
));
843 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".vob"), ED2KFT_VIDEO
));
844 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".wmv"), ED2KFT_VIDEO
));
845 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".xvid"), ED2KFT_VIDEO
));
847 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".bmp"), ED2KFT_IMAGE
));
848 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".dcx"), ED2KFT_IMAGE
));
849 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".emf"), ED2KFT_IMAGE
));
850 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".gif"), ED2KFT_IMAGE
));
851 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".ico"), ED2KFT_IMAGE
));
852 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".jpeg"), ED2KFT_IMAGE
));
853 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".jpg"), ED2KFT_IMAGE
));
854 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".pct"), ED2KFT_IMAGE
));
855 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".pcx"), ED2KFT_IMAGE
));
856 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".pic"), ED2KFT_IMAGE
));
857 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".pict"), ED2KFT_IMAGE
));
858 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".png"), ED2KFT_IMAGE
));
859 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".psd"), ED2KFT_IMAGE
));
860 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".psp"), ED2KFT_IMAGE
));
861 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".tga"), ED2KFT_IMAGE
));
862 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".tif"), ED2KFT_IMAGE
));
863 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".tiff"), ED2KFT_IMAGE
));
864 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".wmf"), ED2KFT_IMAGE
));
865 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".xif"), ED2KFT_IMAGE
));
867 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".7z"), ED2KFT_ARCHIVE
));
868 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".ace"), ED2KFT_ARCHIVE
));
869 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".arj"), ED2KFT_ARCHIVE
));
870 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".bz2"), ED2KFT_ARCHIVE
));
871 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".cab"), ED2KFT_ARCHIVE
));
872 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".gz"), ED2KFT_ARCHIVE
));
873 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".hqx"), ED2KFT_ARCHIVE
));
874 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".lha"), ED2KFT_ARCHIVE
));
875 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".msi"), ED2KFT_ARCHIVE
));
876 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".rar"), ED2KFT_ARCHIVE
));
877 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".sea"), ED2KFT_ARCHIVE
));
878 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".sit"), ED2KFT_ARCHIVE
));
879 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".tar"), ED2KFT_ARCHIVE
));
880 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".tgz"), ED2KFT_ARCHIVE
));
881 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".uc2"), ED2KFT_ARCHIVE
));
882 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".zip"), ED2KFT_ARCHIVE
));
884 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".bat"), ED2KFT_PROGRAM
));
885 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".cmd"), ED2KFT_PROGRAM
));
886 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".com"), ED2KFT_PROGRAM
));
887 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".exe"), ED2KFT_PROGRAM
));
889 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".bin"), ED2KFT_CDIMAGE
));
890 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".bwa"), ED2KFT_CDIMAGE
));
891 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".bwi"), ED2KFT_CDIMAGE
));
892 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".bws"), ED2KFT_CDIMAGE
));
893 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".bwt"), ED2KFT_CDIMAGE
));
894 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".ccd"), ED2KFT_CDIMAGE
));
895 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".cue"), ED2KFT_CDIMAGE
));
896 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".dmg"), ED2KFT_CDIMAGE
));
897 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".dmz"), ED2KFT_CDIMAGE
));
898 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".img"), ED2KFT_CDIMAGE
));
899 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".iso"), ED2KFT_CDIMAGE
));
900 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mdf"), ED2KFT_CDIMAGE
));
901 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".mds"), ED2KFT_CDIMAGE
));
902 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".nrg"), ED2KFT_CDIMAGE
));
903 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".sub"), ED2KFT_CDIMAGE
));
904 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".toast"), ED2KFT_CDIMAGE
));
906 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".chm"), ED2KFT_DOCUMENT
));
907 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".css"), ED2KFT_DOCUMENT
));
908 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".diz"), ED2KFT_DOCUMENT
));
909 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".doc"), ED2KFT_DOCUMENT
));
910 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".dot"), ED2KFT_DOCUMENT
));
911 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".hlp"), ED2KFT_DOCUMENT
));
912 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".htm"), ED2KFT_DOCUMENT
));
913 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".html"), ED2KFT_DOCUMENT
));
914 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".nfo"), ED2KFT_DOCUMENT
));
915 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".pdf"), ED2KFT_DOCUMENT
));
916 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".pps"), ED2KFT_DOCUMENT
));
917 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".ppt"), ED2KFT_DOCUMENT
));
918 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".ps"), ED2KFT_DOCUMENT
));
919 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".rtf"), ED2KFT_DOCUMENT
));
920 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".wri"), ED2KFT_DOCUMENT
));
921 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".txt"), ED2KFT_DOCUMENT
));
922 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".xls"), ED2KFT_DOCUMENT
));
923 ED2KFileTypesMap
.insert(SED2KFileTypeMapElement(wxT(".xlt"), ED2KFT_DOCUMENT
));
928 // get the list initialized *before* any code is accessing it
929 CED2KFileTypes theED2KFileTypes
;
931 EED2KFileType
GetED2KFileTypeID(const CPath
& fileName
)
933 const wxString ext
= fileName
.GetExt().Lower();
938 SED2KFileTypeMap::iterator it
= ED2KFileTypesMap
.find(wxT(".") + ext
);
939 if (it
!= ED2KFileTypesMap
.end()) {
940 return it
->second
.GetType();
947 // Retuns the ed2k file type term which is to be used in server searches
948 wxString
GetED2KFileTypeSearchTerm(EED2KFileType iFileID
)
950 if (iFileID
== ED2KFT_AUDIO
) return ED2KFTSTR_AUDIO
;
951 if (iFileID
== ED2KFT_VIDEO
) return ED2KFTSTR_VIDEO
;
952 if (iFileID
== ED2KFT_IMAGE
) return ED2KFTSTR_IMAGE
;
953 if (iFileID
== ED2KFT_DOCUMENT
) return ED2KFTSTR_DOCUMENT
;
954 if (iFileID
== ED2KFT_PROGRAM
) return ED2KFTSTR_PROGRAM
;
955 // NOTE: Archives and CD-Images are published with file type "Pro"
956 if (iFileID
== ED2KFT_ARCHIVE
) return ED2KFTSTR_PROGRAM
;
957 if (iFileID
== ED2KFT_CDIMAGE
) return ED2KFTSTR_PROGRAM
;
959 return wxEmptyString
;
963 // Returns a file type which is used eMule internally only, examining the extention of the given filename
964 wxString
GetFileTypeByName(const CPath
& fileName
)
966 EED2KFileType iFileType
= GetED2KFileTypeID(fileName
);
968 case ED2KFT_AUDIO
: return ED2KFTSTR_AUDIO
;
969 case ED2KFT_VIDEO
: return ED2KFTSTR_VIDEO
;
970 case ED2KFT_IMAGE
: return ED2KFTSTR_IMAGE
;
971 case ED2KFT_DOCUMENT
: return ED2KFTSTR_DOCUMENT
;
972 case ED2KFT_PROGRAM
: return ED2KFTSTR_PROGRAM
;
973 case ED2KFT_ARCHIVE
: return ED2KFTSTR_ARCHIVE
;
974 case ED2KFT_CDIMAGE
: return ED2KFTSTR_CDIMAGE
;
975 default: return wxEmptyString
;
980 // Retuns the ed2k file type integer ID which is to be used for publishing+searching
981 EED2KFileType
GetED2KFileTypeSearchID(EED2KFileType iFileID
)
984 case ED2KFT_AUDIO
: return ED2KFT_AUDIO
;
985 case ED2KFT_VIDEO
: return ED2KFT_VIDEO
;
986 case ED2KFT_IMAGE
: return ED2KFT_IMAGE
;
987 case ED2KFT_DOCUMENT
: return ED2KFT_DOCUMENT
;
988 case ED2KFT_PROGRAM
: return ED2KFT_PROGRAM
;
989 // NOTE: Archives and CD-Images are published+searched with file type "Pro"
990 // NOTE: If this gets changed, the function 'GetED2KFileTypeSearchTerm' also needs to get updated!
991 case ED2KFT_ARCHIVE
: return ED2KFT_PROGRAM
;
992 case ED2KFT_CDIMAGE
: return ED2KFT_PROGRAM
;
993 default: return ED2KFT_ANY
;
999 * Dumps a buffer to a wxString
1001 wxString
DumpMemToStr(const void *buff
, int n
, const wxString
& msg
, bool ok
)
1003 const unsigned char *p
= (const unsigned char *)buff
;
1004 int lines
= (n
+ 15)/ 16;
1007 // Allocate aproximetly what is needed
1008 result
.Alloc( ( lines
+ 1 ) * 80 );
1009 if ( !msg
.IsEmpty() ) {
1010 result
+= msg
+ wxT(" - ok=") + ( ok
? wxT("true, ") : wxT("false, ") );
1013 result
+= wxString::Format( wxT("%d bytes\n"), n
);
1014 for ( int i
= 0; i
< lines
; ++i
) {
1016 result
+= wxString::Format( wxT("%08x "), i
* 16 );
1018 // Show two columns of hex-values
1019 for ( int j
= 0; j
< 2; ++j
) {
1020 for ( int k
= 0; k
< 8; ++k
) {
1021 int pos
= 16 * i
+ 8 * j
+ k
;
1024 result
+= wxString::Format( wxT("%02x "), p
[pos
] );
1032 // Show a column of ascii-values
1033 for ( int k
= 0; k
< 16; ++k
) {
1034 int pos
= 16 * i
+ k
;
1037 if ( isspace( p
[pos
] ) ) {
1039 } else if ( !isgraph( p
[pos
] ) ) {
1042 result
+= (wxChar
)p
[pos
];
1048 result
+= wxT("|\n");
1057 * Dumps a buffer to stdout
1059 void DumpMem(const void *buff
, int n
, const wxString
& msg
, bool ok
)
1061 printf("%s\n", (const char*)unicode2char(DumpMemToStr( buff
, n
, msg
, ok
)) );
1066 // Dump mem in dword format
1067 void DumpMem_DW(const uint32
*ptr
, int count
)
1069 for(int i
= 0; i
< count
; i
++) {
1070 printf("%08x ", ptr
[i
]);
1071 if ( (i
% 4) == 3) printf("\n");
1077 void MilliSleep(uint32 msecs
)
1085 struct timespec waittime
;
1086 waittime
.tv_sec
= 0;
1087 waittime
.tv_nsec
= msecs
* 1000 /*micro*/* 1000 /*nano*/;
1088 struct timespec remtime
;
1089 while ((nanosleep(&waittime
,&remtime
)==-1) && (errno
== EINTR
)) {
1090 memcpy(&waittime
,&remtime
,sizeof(struct timespec
));
1094 wxMilliSleep(msecs
);
1099 wxString
GetConfigDir()
1102 static wxString configPath
;
1104 if (configPath
.IsEmpty()) {
1106 // "Portable aMule" - Use aMule from an external USB drive
1107 // Check for ./config/amule.conf and use this configuration if found
1108 const wxString configDir
= JoinPaths(wxFileName::GetCwd(), wxT("config"));
1109 const wxString configFile
= JoinPaths(configDir
, wxT("amule.conf"));
1111 if (CPath::DirExists(configDir
) && CPath::FileExists(configFile
)) {
1112 AddLogLineM(true, CFormat(wxT("Using configDir: %s")) % configDir
);
1114 configPath
= configDir
;
1116 configPath
= wxStandardPaths::Get().GetUserDataDir();
1119 configPath
= wxStandardPaths::Get().GetUserDataDir();
1122 configPath
+= wxFileName::GetPathSeparator();
1129 void InitCustomLanguages()
1131 wxLanguageInfo CustomLanguage
;
1132 CustomLanguage
.Language
= wxLANGUAGE_ITALIAN_NAPOLITAN
;
1133 CustomLanguage
.CanonicalName
= wxT("it_NA");
1134 CustomLanguage
.Description
= wxT("sNeo's Custom Napolitan Language");
1135 wxLocale::AddLanguage(CustomLanguage
);
1139 void InitLocale(wxLocale
& locale
, int language
)
1141 int language_flags
= 0;
1142 if (language
!= wxLANGUAGE_CUSTOM
&& language
!= wxLANGUAGE_ITALIAN_NAPOLITAN
) {
1143 language_flags
= wxLOCALE_LOAD_DEFAULT
| wxLOCALE_CONV_ENCODING
;
1146 locale
.Init(language
,language_flags
);
1148 if (language
!= wxLANGUAGE_CUSTOM
) {
1150 #if defined(__WXMAC__)
1151 wxStandardPathsBase
&spb(wxStandardPaths::Get());
1152 locale
.AddCatalogLookupPathPrefix(JoinPaths(spb
.GetDataDir(), wxT("locale")));
1154 locale
.AddCatalog(wxT(PACKAGE
));
1157 locale
.AddCatalogLookupPathPrefix(GetConfigDir());
1158 locale
.AddCatalog(wxT("custom"));
1163 int StrLang2wx(const wxString
& language
)
1165 // get rid of possible encoding and modifier
1166 wxString
lang(language
.BeforeFirst('.').BeforeFirst('@'));
1168 if (!lang
.IsEmpty()) {
1169 const wxLanguageInfo
*lng
= wxLocale::FindLanguageInfo(lang
);
1171 return lng
->Language
;
1173 return wxLANGUAGE_DEFAULT
;
1176 return wxLANGUAGE_DEFAULT
;
1181 wxString
wxLang2Str(const int lang
)
1183 if (lang
!= wxLANGUAGE_DEFAULT
) {
1184 const wxLanguageInfo
*lng
= wxLocale::GetLanguageInfo(lang
);
1186 return lng
->CanonicalName
;
1188 return wxEmptyString
;
1191 return wxEmptyString
;
1195 wxString
GetPassword() {
1196 wxString pass_plain
;
1199 pass_plain
= char2unicode(getpass("Enter password for mule connection: "));
1201 //#warning This way, pass enter is not hidden on windows. Bad thing.
1204 printf("Enter password for mule connection: \n");
1206 fgets(temp_str
, 512, stdin
);
1207 temp_str
[strlen(temp_str
)-1] = '\0';
1208 pass_plain
= char2unicode(temp_str
);
1210 wxCHECK2(password
.Decode(MD5Sum(pass_plain
).GetHash()), /* Do nothing. */ );
1211 // MD5 hash for an empty string, according to rfc1321.
1212 if (password
.Encode() == wxT("D41D8CD98F00B204E9800998ECF8427E")) {
1213 printf("No empty password allowed.\n");
1214 return GetPassword();
1218 return password
.Encode();
1221 #if !defined(AMULE_DAEMON) && (!defined(EC_REMOTE) || defined(CLIENT_GUI))
1223 bool IsLocaleAvailable(int id
)
1225 // This supresses error-messages about invalid locales.
1226 wxLogNull logTarget
;
1227 wxLocale locale_to_check
;
1229 if (id
== wxLANGUAGE_DEFAULT
|| id
== theApp
->m_locale
.GetLanguage())
1232 InitLocale(locale_to_check
, id
);
1233 if (locale_to_check
.IsOk()) {
1234 return locale_to_check
.IsLoaded(wxT(PACKAGE
));
1240 #endif /* !AMULE_DEAMON && (!EC_REMOTE || CLIENT_GUI) */
1241 // File_checked_for_headers