Upstream tarball 20080418
[amule.git] / src / OtherFunctions.cpp
blob400ceec7174e01d007168f54b34d3652f4aa8a43
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
62 wxString GetMuleVersion()
64 wxString ver(wxT(VERSION));
66 ver += wxT(" using ");
69 // Figure out the wx build-type
70 #ifdef __WXGTK__
71 #ifdef __WXGTK20__
72 ver += wxT("wxGTK2");
73 #else
74 ver += wxT("wxGTK1");
75 #endif
76 #elif defined(__WXMAC__)
77 ver += wxT("wxMac");
78 #elif defined(__WXMSW__)
79 ver += wxT("wxMSW");
80 #elif defined(__WXCOCOA__)
81 ver += wxT("wxCocoa");
82 #endif
84 ver += wxString::Format(wxT(" v%d.%d.%d"), wxMAJOR_VERSION, wxMINOR_VERSION, wxRELEASE_NUMBER );
86 #if defined(__WXDEBUG__)
87 ver += wxT(" (Debugging)");
88 #endif
90 #ifdef SVNDATE
91 ver += wxString::Format( wxT(" (Snapshot: %s)"), wxT(SVNDATE));
92 #endif
94 return ver;
98 wxString GetFullMuleVersion()
100 #ifdef AMULE_DAEMON
101 wxString app = wxT("aMuled");
102 #elif defined(CLIENT_GUI)
103 wxString app = wxT("Remote aMule-GUI");
104 #else
105 wxString app = wxT("aMule");
106 #endif
108 return app + wxT(" ") + GetMuleVersion();
112 // Formats a filesize in bytes to make it suitable for displaying
113 wxString CastItoXBytes( uint64 count )
116 if (count < 1024)
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") ;
124 else
125 return wxString::Format( wxT("%.3f "), (float)count/1099511627776LL) + _("TB") ;
127 return _("Error");
131 wxString CastItoIShort(uint64 count)
134 if (count < 1000)
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");
145 return _("Error");
149 wxString CastItoSpeed(uint32 bytes)
151 if (bytes < 1024)
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");
155 else
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)
163 if (count < 60) {
164 if (!msecs) {
165 return wxString::Format(
166 wxT("%02") wxLongLongFmtSpec wxT("u "),
167 count) + _("secs");
168 } else {
169 return wxString::Format(
170 wxT("%.3f"),
171 (count + ((float)msecs/1000))) + _("secs");
173 } else if (count < 3600) {
174 return wxString::Format(
175 wxT("%")
176 wxLongLongFmtSpec wxT("u:%02")
177 wxLongLongFmtSpec wxT("u "),
178 count/60,
179 (count % 60)) + _("mins");
180 } else if (count < 86400) {
181 return wxString::Format(
182 wxT("%")
183 wxLongLongFmtSpec wxT("u:%02")
184 wxLongLongFmtSpec wxT("u "),
185 count/3600,
186 (count % 3600)/60) + _("hours");
187 } else {
188 return wxString::Format(
189 wxT("%")
190 wxLongLongFmtSpec wxT("u %s %02")
191 wxLongLongFmtSpec wxT("u:%02")
192 wxLongLongFmtSpec wxT("u "),
193 count/86400,
194 _("Days"),
195 (count % 86400)/3600,
196 (count % 3600)/60) + _("hours");
199 return _("Error");
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)
223 switch ( type ) {
224 case ftVideo:
225 if (translated) {
226 return _("Videos");
227 } else {
228 return wxT("Videos");
230 break;
231 case ftAudio:
232 if (translated) {
233 return _("Audio");
234 } else {
235 return wxT("Audio");
237 break;
238 case ftArchive:
239 if (translated) {
240 return _("Archives");
241 } else {
242 return wxT("Archives");
244 break;
245 case ftCDImage:
246 if (translated) {
247 return _("CD-Images");
248 } else {
249 return wxT("CD-Images");
251 break;
252 case ftPicture:
253 if (translated) {
254 return _("Pictures");
255 } else {
256 return wxT("Pictures");
258 break;
259 case ftText:
260 if (translated) {
261 return _("Texts");
262 } else {
263 return wxT("Texts");
265 break;
266 case ftProgram:
267 if (translated) {
268 return _("Programs");
269 } else {
270 return wxT("Programs");
272 break;
273 default:
274 if (translated) {
275 return _("Any");
276 } else {
277 return wxT("Any");
279 break;
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()
294 int maxconn = -1;
295 #ifdef __WXMSW__
296 // Try to get the max connection value in the registry
297 wxRegKey key( wxT("HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\Services\\VxD\\MSTCP\\MaxConnections") );
298 wxString value;
299 if ( key.Exists() ) {
300 value = key.QueryDefaultValue();
302 if ( !value.IsEmpty() && value.IsNumber() ) {
303 long mc;
304 value.ToLong(&mc);
305 maxconn = (int)mc;
306 } else {
307 switch (wxGetOsVersion()) {
308 case wxOS_WINDOWS_9X:
309 // This includes all Win9x versions
310 maxconn = 50;
311 break;
312 case wxOS_WINDOWS_NT:
313 // This includes NT based windows
314 maxconn = 500;
315 break;
316 default:
317 // Anything else. Let aMule decide...
318 break;
321 #else
322 // Any other OS can just use the default number of connections
323 #endif
324 return maxconn;
328 // Return the text assosiated with a rating of a file
329 wxString GetRateString(uint16 rate)
331 switch ( 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};
356 int size;
358 switch(type) {
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;
365 return size;
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] = {
374 { wxT('0'), 0x0 },
375 { wxT('1'), 0x1 },
376 { wxT('2'), 0x2 },
377 { wxT('3'), 0x3 },
378 { wxT('4'), 0x4 },
379 { wxT('5'), 0x5 },
380 { wxT('6'), 0x6 },
381 { wxT('7'), 0x7 },
382 { wxT('8'), 0x8 },
383 { wxT('9'), 0x9 },
384 { wxT(':'), 0x9 },
385 { wxT(';'), 0x9 },
386 { wxT('<'), 0x9 },
387 { wxT('='), 0x9 },
388 { wxT('>'), 0x9 },
389 { wxT('?'), 0x9 },
390 { wxT('@'), 0x9 },
391 { wxT('A'), 0xA },
392 { wxT('B'), 0xB },
393 { wxT('C'), 0xC },
394 { wxT('D'), 0xD },
395 { wxT('E'), 0xE },
396 { wxT('F'), 0xF }
400 // Returns a BASE16 encoded byte array
402 // [In]
403 // buffer: Pointer to byte array
404 // bufLen: Lenght of buffer array
406 // [Return]
407 // wxString object with BASE16 encoded byte array
408 wxString EncodeBase16(const unsigned char* buffer, unsigned int bufLen)
410 wxString Base16Buff;
412 for(unsigned int i = 0; i < bufLen; ++i) {
413 Base16Buff += base16Chars[buffer[i] >> 4];
414 Base16Buff += base16Chars[buffer[i] & 0xf];
417 return Base16Buff;
421 // Decodes a BASE16 string into a byte array
423 // [In]
424 // base16Buffer: String containing BASE16
425 // base16BufLen: Lenght BASE16 coded string's length
427 // [Out]
428 // buffer: byte array containing decoded string
429 unsigned int DecodeBase16(const wxString &base16Buffer, unsigned int base16BufLen, byte *buffer)
431 if (base16BufLen & 1) {
432 return 0;
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);
446 return ret;
450 // Returns a BASE32 encoded byte array
452 // [In]
453 // buffer: Pointer to byte array
454 // bufLen: Lenght of buffer array
456 // [Return]
457 // wxString object with BASE32 encoded byte array
458 wxString EncodeBase32(const unsigned char* buffer, unsigned int bufLen)
460 wxString Base32Buff;
461 unsigned int i, index;
462 unsigned char word;
464 for(i = 0, index = 0; i < bufLen;) {
465 // Is the current word going to span a byte boundary?
466 if (index > 3) {
467 word = (buffer[i] & (0xFF >> index));
468 index = (index + 5) % 8;
469 word <<= index;
470 if (i < bufLen - 1) {
471 word |= buffer[i + 1] >> (8 - index);
473 ++i;
474 } else {
475 word = (buffer[i] >> (8 - (index + 5))) & 0x1F;
476 index = (index + 5) % 8;
477 if (index == 0) {
478 ++i;
481 Base32Buff += (char) base32Chars[word];
484 return Base32Buff;
488 // Decodes a BASE32 string into a byte array
490 // [In]
491 // base32Buffer: String containing BASE32
492 // base32BufLen: Lenght BASE32 coded string's length
494 // [Out]
495 // buffer: byte array containing decoded string
496 // [Return]
497 // nDecodeLen:
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) {
503 ++nDecodeLen;
505 if (base32BufLen == 0) {
506 return nDecodeLen;
508 if (nDecodeLen > base32BufLen) {
509 return 0;
512 uint32 nBits = 0;
513 int nCount = 0;
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 );
524 } else {
525 return 0;
527 nCount += 5;
528 if (nCount >= 8)
530 *buffer++ = (byte)( nBits >> (nCount - 8) );
531 nCount -= 8;
533 nBits <<= 5;
536 return nDecodeLen;
541 * base64.c
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;
564 wxString strHeader;
566 if( !strHeaderLine.IsEmpty() ) {
567 strHeader = wxT("-----BEGIN ") + strHeaderLine + wxT("-----");
568 if( g_fUseCRLF ) {
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
579 pbBufferOut.Alloc(
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;
588 while( nDiv > 0 ) {
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];
593 pIn += 3;
594 nDiv--;
595 nChars += 4;
596 if( nChars >= g_nCharsPerLine && g_nCharsPerLine != 0 ) {
597 nChars = 0;
598 if( g_fUseCRLF ) {
599 pbBufferOut += wxT("\r");
601 pbBufferOut += wxT("\n");
604 switch( nRem ) {
605 case 2:
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("=");
610 nChars += 4;
611 if( nChars >= g_nCharsPerLine && g_nCharsPerLine != 0 ) {
612 nChars = 0;
613 if( g_fUseCRLF ) {
614 pbBufferOut += wxT("\r");
616 pbBufferOut += wxT("\n");
618 break;
619 case 1:
620 pbBufferOut += to_b64[ (pIn[0] >> 2) & 0x3f];
621 pbBufferOut += to_b64[ (pIn[0] << 4) & 0x30];
622 pbBufferOut += wxT("=");
623 pbBufferOut += wxT("=");
624 nChars += 4;
625 if( nChars >= g_nCharsPerLine && g_nCharsPerLine != 0 ) {
626 nChars = 0;
627 if( g_fUseCRLF ) {
628 pbBufferOut += wxT("\r");
630 pbBufferOut += wxT("\n");
632 break;
635 if( nRem > 0 ) {
636 if( nChars > 0 ) {
637 if( g_fUseCRLF ) {
638 pbBufferOut += wxT("\r");
640 pbBufferOut += wxT("\n");
644 if( !strHeaderLine.IsEmpty() ) {
645 pbBufferOut = wxT("-----END ") + strHeaderLine + wxT("-----");
646 if( g_fUseCRLF ) {
647 pbBufferOut += wxT("\r");
649 pbBufferOut += wxT("\n");
652 return pbBufferOut;
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;
660 unsigned int i = 0;
662 if (base64BufLen == 0) {
663 *buffer = 0;
664 nData = 1;
667 for(unsigned int j = 0; j < base64BufLen; ++j) {
668 wxChar c = base64Buffer[j];
669 wxChar bits = wxT('z');
670 if( z > 0 ) {
671 if(c == wxT('\n')) {
672 z = 0;
675 else if(c >= wxT('A') && c <= wxT('Z')) {
676 bits = c - wxT('A');
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('+')) {
685 bits = (wxChar)62;
687 else if(c == wxT('/')) {
688 bits = (wxChar)63;
690 else if(c == wxT('-')) {
691 z = 1;
693 else if(c == wxT('=')) {
694 break;
695 } else {
696 bits = wxT('y');
699 // Skips anything that was not recognized
700 // as a base64 valid char ('y' or 'z')
701 if (bits < (wxChar)64) {
702 switch (nData++) {
703 case 0:
704 buffer[i+0] = (bits << 2) & 0xfc;
705 break;
706 case 1:
707 buffer[i+0] |= (bits >> 4) & 0x03;
708 buffer[i+1] = (bits << 4) & 0xf0;
709 break;
710 case 2:
711 buffer[i+1] |= (bits >> 2) & 0x0f;
712 buffer[i+2] = (bits << 6) & 0xc0;
713 break;
714 case 3:
715 buffer[i+2] |= bits & 0x3f;
716 break;
718 if (nData == 4) {
719 nData = 0;
720 i += 3;
724 if (nData == 1) {
725 // Syntax error or buffer was empty
726 *buffer = 0;
727 nData = 0;
728 i = 0;
729 } else {
730 buffer[i+nData] = 0;
733 return i + nData;
737 // Returns the text assosiated with a category type
738 wxString GetCatTitle(int catid)
740 switch (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{
768 public:
769 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();
930 if (ext.IsEmpty()) {
931 return ED2KFT_ANY;
934 SED2KFileTypeMap::iterator it = ED2KFileTypesMap.find(wxT(".") + ext);
935 if (it != ED2KFileTypesMap.end()) {
936 return it->second.GetType();
937 } else {
938 return ED2KFT_ANY;
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);
963 switch (iFileType) {
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)
979 switch (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;
1002 wxString result;
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) {
1011 // Show address
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;
1019 if ( pos < n ) {
1020 result += wxString::Format( wxT("%02x "), p[pos] );
1021 } else {
1022 result += wxT(" ");
1025 result += wxT(" ");
1027 result += wxT("|");
1028 // Show a column of ascii-values
1029 for ( int k = 0; k < 16; ++k) {
1030 int pos = 16 * i + k;
1032 if ( pos < n ) {
1033 if ( isspace( p[pos] ) ) {
1034 result += wxT(" ");
1035 } else if ( !isgraph( p[pos] ) ) {
1036 result += wxT(".");
1037 } else {
1038 result += (wxChar)p[pos];
1040 } else {
1041 result += wxT(" ");
1044 result += wxT("|\n");
1046 result.Shrink();
1048 return result;
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");
1069 printf("\n");
1073 void MilliSleep(uint32 msecs)
1075 #ifdef __WXBASE__
1076 #ifdef __WXMSW__
1077 if (msecs) {
1078 wxSleep(msecs);
1080 #else
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));
1088 #endif
1089 #else
1090 wxMilliSleep(msecs);
1091 #endif
1095 wxString GetConfigDir()
1097 // Cache the path.
1098 static wxString configPath;
1100 if (configPath.IsEmpty()) {
1101 #ifndef EC_REMOTE
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;
1111 } else {
1112 configPath = wxStandardPaths::Get().GetUserDataDir();
1114 #else
1115 configPath = wxStandardPaths::Get().GetUserDataDir();
1116 #endif
1118 configPath += wxFileName::GetPathSeparator();
1121 return configPath;
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")));
1148 #endif
1149 locale.AddCatalog(wxT(PACKAGE));
1151 } else {
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);
1165 if (lng) {
1166 return lng->Language;
1167 } else {
1168 return wxLANGUAGE_DEFAULT;
1170 } else {
1171 return wxLANGUAGE_DEFAULT;
1176 wxString wxLang2Str(const int lang)
1178 if (lang != wxLANGUAGE_DEFAULT) {
1179 const wxLanguageInfo *lng = wxLocale::GetLanguageInfo(lang);
1180 if (lng) {
1181 return lng->CanonicalName;
1182 } else {
1183 return wxEmptyString;
1185 } else {
1186 return wxEmptyString;
1190 wxString GetPassword() {
1191 wxString pass_plain;
1192 CMD4Hash password;
1193 #ifndef __WXMSW__
1194 pass_plain = char2unicode(getpass("Enter password for mule connection: "));
1195 #else
1196 //#warning This way, pass enter is not hidden on windows. Bad thing.
1197 char temp_str[512];
1198 fflush(stdin);
1199 printf("Enter password for mule connection: \n");
1200 fflush(stdout);
1201 fgets(temp_str, 512, stdin);
1202 temp_str[strlen(temp_str)-1] = '\0';
1203 pass_plain = char2unicode(temp_str);
1204 #endif
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