Upstream tarball 20080304
[amule.git] / src / OtherFunctions.cpp
blob0f5953eb7d47e09c886611cb39d2550e39f3d60d
1 //
2 // This file is part of the aMule Project.
3 //
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 )
6 //
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
9 // respective authors.
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.
20 //
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/utils.h>
32 #include <wx/filename.h> // Needed for wxFileName
33 #include <wx/log.h> // Needed for wxLogNull
35 #ifdef __WXMSW__
36 #include <wx/msw/registry.h> // Do_not_auto_remove
37 #endif
39 #ifdef HAVE_CONFIG_H
40 #include "config.h" // Needed for a number of defines
41 #endif
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>
48 #include "MD4Hash.h"
49 #include "Logger.h"
51 #include "OtherFunctions.h" // Interface declarations
53 #include <map>
55 #ifdef __WXBASE__
56 #include <cerrno>
57 #else
58 #include <wx/utils.h>
59 #endif
61 #if !defined(AMULE_DAEMON) && (!defined(EC_REMOTE) || defined(CLIENT_GUI))
62 #include "amule.h" // Needed for theApp
63 #endif
66 wxString GetMuleVersion()
68 wxString ver(wxT(VERSION));
70 ver += wxT(" using ");
73 // Figure out the wx build-type
74 #ifdef __WXGTK__
75 #ifdef __WXGTK20__
76 ver += wxT("wxGTK2");
77 #else
78 ver += wxT("wxGTK1");
79 #endif
80 #elif defined(__WXMAC__)
81 ver += wxT("wxMac");
82 #elif defined(__WXMSW__)
83 ver += wxT("wxMSW");
84 #elif defined(__WXCOCOA__)
85 ver += wxT("wxCocoa");
86 #endif
88 ver += wxString::Format(wxT(" v%d.%d.%d"), wxMAJOR_VERSION, wxMINOR_VERSION, wxRELEASE_NUMBER );
90 #if defined(__WXDEBUG__)
91 ver += wxT(" (Debugging)");
92 #endif
94 #ifdef SVNDATE
95 ver += wxString::Format( wxT(" (Snapshot: %s)"), wxT(SVNDATE));
96 #endif
98 return ver;
102 wxString GetFullMuleVersion()
104 #ifdef AMULE_DAEMON
105 wxString app = wxT("aMuled");
106 #elif defined(CLIENT_GUI)
107 wxString app = wxT("Remote aMule-GUI");
108 #else
109 wxString app = wxT("aMule");
110 #endif
112 return app + wxT(" ") + GetMuleVersion();
116 // Formats a filesize in bytes to make it suitable for displaying
117 wxString CastItoXBytes( uint64 count )
120 if (count < 1024)
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") ;
128 else
129 return wxString::Format( wxT("%.3f "), (float)count/1099511627776LL) + _("TB") ;
131 return _("Error");
135 wxString CastItoIShort(uint64 count)
138 if (count < 1000)
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");
149 return _("Error");
153 wxString CastItoSpeed(uint32 bytes)
155 if (bytes < 1024)
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");
159 else
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)
167 if (count < 60) {
168 if (!msecs) {
169 return wxString::Format(
170 wxT("%02") wxLongLongFmtSpec wxT("u "),
171 count) + _("secs");
172 } else {
173 return wxString::Format(
174 wxT("%.3f"),
175 (count + ((float)msecs/1000))) + _("secs");
177 } else if (count < 3600) {
178 return wxString::Format(
179 wxT("%")
180 wxLongLongFmtSpec wxT("u:%02")
181 wxLongLongFmtSpec wxT("u "),
182 count/60,
183 (count % 60)) + _("mins");
184 } else if (count < 86400) {
185 return wxString::Format(
186 wxT("%")
187 wxLongLongFmtSpec wxT("u:%02")
188 wxLongLongFmtSpec wxT("u "),
189 count/3600,
190 (count % 3600)/60) + _("hours");
191 } else {
192 return wxString::Format(
193 wxT("%")
194 wxLongLongFmtSpec wxT("u %s %02")
195 wxLongLongFmtSpec wxT("u:%02")
196 wxLongLongFmtSpec wxT("u "),
197 count/86400,
198 _("Days"),
199 (count % 86400)/3600,
200 (count % 3600)/60) + _("hours");
203 return _("Error");
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)
227 switch ( type ) {
228 case ftVideo:
229 if (translated) {
230 return _("Videos");
231 } else {
232 return wxT("Videos");
234 break;
235 case ftAudio:
236 if (translated) {
237 return _("Audio");
238 } else {
239 return wxT("Audio");
241 break;
242 case ftArchive:
243 if (translated) {
244 return _("Archives");
245 } else {
246 return wxT("Archives");
248 break;
249 case ftCDImage:
250 if (translated) {
251 return _("CD-Images");
252 } else {
253 return wxT("CD-Images");
255 break;
256 case ftPicture:
257 if (translated) {
258 return _("Pictures");
259 } else {
260 return wxT("Pictures");
262 break;
263 case ftText:
264 if (translated) {
265 return _("Texts");
266 } else {
267 return wxT("Texts");
269 break;
270 case ftProgram:
271 if (translated) {
272 return _("Programs");
273 } else {
274 return wxT("Programs");
276 break;
277 default:
278 if (translated) {
279 return _("Any");
280 } else {
281 return wxT("Any");
283 break;
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()
298 int maxconn = -1;
299 #ifdef __WXMSW__
300 // Try to get the max connection value in the registry
301 wxRegKey key( wxT("HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\Services\\VxD\\MSTCP\\MaxConnections") );
302 wxString value;
303 if ( key.Exists() ) {
304 value = key.QueryDefaultValue();
306 if ( !value.IsEmpty() && value.IsNumber() ) {
307 long mc;
308 value.ToLong(&mc);
309 maxconn = (int)mc;
310 } else {
311 switch (wxGetOsVersion()) {
312 case wxOS_WINDOWS_9X:
313 // This includes all Win9x versions
314 maxconn = 50;
315 break;
316 case wxOS_WINDOWS_NT:
317 // This includes NT based windows
318 maxconn = 500;
319 break;
320 default:
321 // Anything else. Let aMule decide...
322 break;
325 #else
326 // Any other OS can just use the default number of connections
327 #endif
328 return maxconn;
332 // Return the text assosiated with a rating of a file
333 wxString GetRateString(uint16 rate)
335 switch ( 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};
360 int size;
362 switch(type) {
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;
369 return size;
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] = {
378 { wxT('0'), 0x0 },
379 { wxT('1'), 0x1 },
380 { wxT('2'), 0x2 },
381 { wxT('3'), 0x3 },
382 { wxT('4'), 0x4 },
383 { wxT('5'), 0x5 },
384 { wxT('6'), 0x6 },
385 { wxT('7'), 0x7 },
386 { wxT('8'), 0x8 },
387 { wxT('9'), 0x9 },
388 { wxT(':'), 0x9 },
389 { wxT(';'), 0x9 },
390 { wxT('<'), 0x9 },
391 { wxT('='), 0x9 },
392 { wxT('>'), 0x9 },
393 { wxT('?'), 0x9 },
394 { wxT('@'), 0x9 },
395 { wxT('A'), 0xA },
396 { wxT('B'), 0xB },
397 { wxT('C'), 0xC },
398 { wxT('D'), 0xD },
399 { wxT('E'), 0xE },
400 { wxT('F'), 0xF }
404 // Returns a BASE16 encoded byte array
406 // [In]
407 // buffer: Pointer to byte array
408 // bufLen: Lenght of buffer array
410 // [Return]
411 // wxString object with BASE16 encoded byte array
412 wxString EncodeBase16(const unsigned char* buffer, unsigned int bufLen)
414 wxString Base16Buff;
416 for(unsigned int i = 0; i < bufLen; ++i) {
417 Base16Buff += base16Chars[buffer[i] >> 4];
418 Base16Buff += base16Chars[buffer[i] & 0xf];
421 return Base16Buff;
425 // Decodes a BASE16 string into a byte array
427 // [In]
428 // base16Buffer: String containing BASE16
429 // base16BufLen: Lenght BASE16 coded string's length
431 // [Out]
432 // buffer: byte array containing decoded string
433 unsigned int DecodeBase16(const wxString &base16Buffer, unsigned int base16BufLen, byte *buffer)
435 if (base16BufLen & 1) {
436 return 0;
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);
450 return ret;
454 // Returns a BASE32 encoded byte array
456 // [In]
457 // buffer: Pointer to byte array
458 // bufLen: Lenght of buffer array
460 // [Return]
461 // wxString object with BASE32 encoded byte array
462 wxString EncodeBase32(const unsigned char* buffer, unsigned int bufLen)
464 wxString Base32Buff;
465 unsigned int i, index;
466 unsigned char word;
468 for(i = 0, index = 0; i < bufLen;) {
469 // Is the current word going to span a byte boundary?
470 if (index > 3) {
471 word = (buffer[i] & (0xFF >> index));
472 index = (index + 5) % 8;
473 word <<= index;
474 if (i < bufLen - 1) {
475 word |= buffer[i + 1] >> (8 - index);
477 ++i;
478 } else {
479 word = (buffer[i] >> (8 - (index + 5))) & 0x1F;
480 index = (index + 5) % 8;
481 if (index == 0) {
482 ++i;
485 Base32Buff += (char) base32Chars[word];
488 return Base32Buff;
492 // Decodes a BASE32 string into a byte array
494 // [In]
495 // base32Buffer: String containing BASE32
496 // base32BufLen: Lenght BASE32 coded string's length
498 // [Out]
499 // buffer: byte array containing decoded string
500 // [Return]
501 // nDecodeLen:
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) {
507 ++nDecodeLen;
509 if (base32BufLen == 0) {
510 return nDecodeLen;
512 if (nDecodeLen > base32BufLen) {
513 return 0;
516 uint32 nBits = 0;
517 int nCount = 0;
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 );
528 } else {
529 return 0;
531 nCount += 5;
532 if (nCount >= 8)
534 *buffer++ = (byte)( nBits >> (nCount - 8) );
535 nCount -= 8;
537 nBits <<= 5;
540 return nDecodeLen;
545 * base64.c
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;
568 wxString strHeader;
570 if( !strHeaderLine.IsEmpty() ) {
571 strHeader = wxT("-----BEGIN ") + strHeaderLine + wxT("-----");
572 if( g_fUseCRLF ) {
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
583 pbBufferOut.Alloc(
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;
592 while( nDiv > 0 ) {
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];
597 pIn += 3;
598 nDiv--;
599 nChars += 4;
600 if( nChars >= g_nCharsPerLine && g_nCharsPerLine != 0 ) {
601 nChars = 0;
602 if( g_fUseCRLF ) {
603 pbBufferOut += wxT("\r");
605 pbBufferOut += wxT("\n");
608 switch( nRem ) {
609 case 2:
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("=");
614 nChars += 4;
615 if( nChars >= g_nCharsPerLine && g_nCharsPerLine != 0 ) {
616 nChars = 0;
617 if( g_fUseCRLF ) {
618 pbBufferOut += wxT("\r");
620 pbBufferOut += wxT("\n");
622 break;
623 case 1:
624 pbBufferOut += to_b64[ (pIn[0] >> 2) & 0x3f];
625 pbBufferOut += to_b64[ (pIn[0] << 4) & 0x30];
626 pbBufferOut += wxT("=");
627 pbBufferOut += wxT("=");
628 nChars += 4;
629 if( nChars >= g_nCharsPerLine && g_nCharsPerLine != 0 ) {
630 nChars = 0;
631 if( g_fUseCRLF ) {
632 pbBufferOut += wxT("\r");
634 pbBufferOut += wxT("\n");
636 break;
639 if( nRem > 0 ) {
640 if( nChars > 0 ) {
641 if( g_fUseCRLF ) {
642 pbBufferOut += wxT("\r");
644 pbBufferOut += wxT("\n");
648 if( !strHeaderLine.IsEmpty() ) {
649 pbBufferOut = wxT("-----END ") + strHeaderLine + wxT("-----");
650 if( g_fUseCRLF ) {
651 pbBufferOut += wxT("\r");
653 pbBufferOut += wxT("\n");
656 return pbBufferOut;
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;
664 unsigned int i = 0;
666 if (base64BufLen == 0) {
667 *buffer = 0;
668 nData = 1;
671 for(unsigned int j = 0; j < base64BufLen; ++j) {
672 wxChar c = base64Buffer[j];
673 wxChar bits = wxT('z');
674 if( z > 0 ) {
675 if(c == wxT('\n')) {
676 z = 0;
679 else if(c >= wxT('A') && c <= wxT('Z')) {
680 bits = c - wxT('A');
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('+')) {
689 bits = (wxChar)62;
691 else if(c == wxT('/')) {
692 bits = (wxChar)63;
694 else if(c == wxT('-')) {
695 z = 1;
697 else if(c == wxT('=')) {
698 break;
699 } else {
700 bits = wxT('y');
703 // Skips anything that was not recognized
704 // as a base64 valid char ('y' or 'z')
705 if (bits < (wxChar)64) {
706 switch (nData++) {
707 case 0:
708 buffer[i+0] = (bits << 2) & 0xfc;
709 break;
710 case 1:
711 buffer[i+0] |= (bits >> 4) & 0x03;
712 buffer[i+1] = (bits << 4) & 0xf0;
713 break;
714 case 2:
715 buffer[i+1] |= (bits >> 2) & 0x0f;
716 buffer[i+2] = (bits << 6) & 0xc0;
717 break;
718 case 3:
719 buffer[i+2] |= bits & 0x3f;
720 break;
722 if (nData == 4) {
723 nData = 0;
724 i += 3;
728 if (nData == 1) {
729 // Syntax error or buffer was empty
730 *buffer = 0;
731 nData = 0;
732 i = 0;
733 } else {
734 buffer[i+nData] = 0;
737 return i + nData;
741 // Returns the text assosiated with a category type
742 wxString GetCatTitle(int catid)
744 switch (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{
772 public:
773 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();
934 if (ext.IsEmpty()) {
935 return ED2KFT_ANY;
938 SED2KFileTypeMap::iterator it = ED2KFileTypesMap.find(wxT(".") + ext);
939 if (it != ED2KFileTypesMap.end()) {
940 return it->second.GetType();
941 } else {
942 return ED2KFT_ANY;
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);
967 switch (iFileType) {
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)
983 switch (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;
1006 wxString result;
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) {
1015 // Show address
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;
1023 if ( pos < n ) {
1024 result += wxString::Format( wxT("%02x "), p[pos] );
1025 } else {
1026 result += wxT(" ");
1029 result += wxT(" ");
1031 result += wxT("|");
1032 // Show a column of ascii-values
1033 for ( int k = 0; k < 16; ++k) {
1034 int pos = 16 * i + k;
1036 if ( pos < n ) {
1037 if ( isspace( p[pos] ) ) {
1038 result += wxT(" ");
1039 } else if ( !isgraph( p[pos] ) ) {
1040 result += wxT(".");
1041 } else {
1042 result += (wxChar)p[pos];
1044 } else {
1045 result += wxT(" ");
1048 result += wxT("|\n");
1050 result.Shrink();
1052 return result;
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");
1073 printf("\n");
1077 void MilliSleep(uint32 msecs)
1079 #ifdef __WXBASE__
1080 #ifdef __WXMSW__
1081 if (msecs) {
1082 wxSleep(msecs);
1084 #else
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));
1092 #endif
1093 #else
1094 wxMilliSleep(msecs);
1095 #endif
1099 wxString GetConfigDir()
1101 // Cache the path.
1102 static wxString configPath;
1104 if (configPath.IsEmpty()) {
1105 #ifndef EC_REMOTE
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;
1115 } else {
1116 configPath = wxStandardPaths::Get().GetUserDataDir();
1118 #else
1119 configPath = wxStandardPaths::Get().GetUserDataDir();
1120 #endif
1122 configPath += wxFileName::GetPathSeparator();
1125 return configPath;
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")));
1153 #endif
1154 locale.AddCatalog(wxT(PACKAGE));
1156 } else {
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);
1170 if (lng) {
1171 return lng->Language;
1172 } else {
1173 return wxLANGUAGE_DEFAULT;
1175 } else {
1176 return wxLANGUAGE_DEFAULT;
1181 wxString wxLang2Str(const int lang)
1183 if (lang != wxLANGUAGE_DEFAULT) {
1184 const wxLanguageInfo *lng = wxLocale::GetLanguageInfo(lang);
1185 if (lng) {
1186 return lng->CanonicalName;
1187 } else {
1188 return wxEmptyString;
1190 } else {
1191 return wxEmptyString;
1195 wxString GetPassword() {
1196 wxString pass_plain;
1197 CMD4Hash password;
1198 #ifndef __WXMSW__
1199 pass_plain = char2unicode(getpass("Enter password for mule connection: "));
1200 #else
1201 //#warning This way, pass enter is not hidden on windows. Bad thing.
1202 char temp_str[512];
1203 fflush(stdin);
1204 printf("Enter password for mule connection: \n");
1205 fflush(stdout);
1206 fgets(temp_str, 512, stdin);
1207 temp_str[strlen(temp_str)-1] = '\0';
1208 pass_plain = char2unicode(temp_str);
1209 #endif
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())
1230 return true;
1232 InitLocale(locale_to_check, id);
1233 if (locale_to_check.IsOk()) {
1234 return locale_to_check.IsLoaded(wxT(PACKAGE));
1235 } else {
1236 return false;
1240 #endif /* !AMULE_DEAMON && (!EC_REMOTE || CLIENT_GUI) */
1241 // File_checked_for_headers