1 /***************************************************************************
2 * common.cpp
3 *
4 * Wed Sep 6 22:19:52 2006
5 * Copyright 2006 liubin,China
6 * Email multiget@gmail.com
7 ****************************************************************************/
8
9 /*
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 */
24
25 #include "common.h"
26 #include "speedctrl.h"
27 #include "mirroradmin.h"
28
29
30 #include <pthread.h>
31
32 #ifdef WIN32
33 #include <windows.h>
34
CheckForRegisterValue(HKEY syskey,std::string key,std::string keyname,std::string & value)35 bool CheckForRegisterValue(
36 HKEY syskey, std::string key, std::string keyname, std::string &value )
37 {
38 HKEY phkResult;
39 ULONG lRet = RegOpenKeyEx( syskey, key.c_str(), 0, KEY_READ, &phkResult );
40
41 if ( ERROR_SUCCESS != lRet )
42 {
43 return false;
44 }
45 else
46 {
47 BYTE valueBuf[ 1024 ] = {0};
48 ULONG valuesize = 1024;
49 DWORD valtype;
50 lRet = RegQueryValueEx( phkResult, keyname.c_str(), NULL, &valtype, valueBuf, &valuesize );
51
52 if ( ERROR_SUCCESS != lRet )
53 {
54 RegCloseKey( phkResult );
55 return false;
56 }
57 else if ( REG_SZ != valtype )
58 {
59 RegCloseKey( phkResult );
60 return false;
61 }
62 else
63 {
64 value = std::string( ( char* ) valueBuf );
65 RegCloseKey( phkResult );
66 return true;
67 }
68 }
69 }
70
71
72 #endif
73
GetUserHome(std::string & homedir)74 bool GetUserHome( std::string& homedir )
75 {
76 #ifdef WIN32
77
78 HKEY syskey = HKEY_CURRENT_USER;
79 std::string Key =
80 ( "Software\\microsoft\\windows\\CurrentVersion\\Explorer\\Shell Folders" );
81 std::string KeyName = ( "Personal" );
82
83 if ( !CheckForRegisterValue( syskey, Key, KeyName, homedir ) )
84 {
85 return false;
86 }
87 else
88 {
89 return true;
90 }
91
92 #else
93 char* userroot = getenv( "HOME" );
94
95 if ( userroot == NULL )
96 return false;
97
98 homedir = std::string( userroot );
99
100 return true;
101
102 #endif
103 }
104
GetTempDir(std::string & tempdir)105 bool GetTempDir( std::string& tempdir )
106 {
107 std::string homedir;
108
109 if ( GetUserHome( homedir ) )
110 {
111 #ifdef WIN32
112 tempdir = homedir + "\\MultiGetTemp";
113 #else
114
115 tempdir = homedir + "/.MultiGet";
116 #endif
117
118 return true;
119 }
120 else
121 {
122 return false;
123 }
124 }
125
126 //trim space at head and rear
Trim(std::string & str)127 void Trim( std::string& str )
128 {
129 std::string::size_type pos = str.find_last_not_of( ' ' );
130
131 if ( pos != std::string::npos )
132 {
133 str.erase( pos + 1 );
134 pos = str.find_first_not_of( ' ' );
135
136 if ( pos != std::string::npos )
137 str.erase( 0, pos );
138 }
139 else
140 str.erase( str.begin(), str.end() );
141 }
142
143 std::vector<_syslog> gSysLog;
144 std::vector<_tasklog> gTaskLog;
145 pthread_mutex_t gTaskLogMutex = PTHREAD_MUTEX_INITIALIZER;
146
147 std::list<std::string> gSavePathHistory;
148 //config
149 int gRunTaskNum = 5;
150 int gConnectTimeOut = 30;
151 int gReceiveTimeOut = 30;
152 int gTaskDefThread = 5;
153 int gRetryTime = 99;
154 int gRetryWait = 30;
155 bool gbIgnoreExt = false;
156 bool gbMonitorExt = false;
157 bool gbShowDropWin = true;
158 bool gbMonitorClip = true;
159 bool gbSoundTip = false;
160 bool gbAutoMd5 = true;
161 bool gbComfirmDelete = true; //10/22
162 int gDropWinX = -1;
163 int gDropWinY = -1;
164
165 _SPEED_MODE gSpeedMode = _SPEED_UNLIMIT;
166
167 std::string gDefSavePath;
168 std::string gDefFtpPass = "multiget@gmail.com";
169 std::string gsMonitorExt = ".bin,.exe,.rar .ico .jpg .png;.bmp;.gz";
170 std::string gsIgnoreExt = ".htm .html .shtml";
171
172 CSpeedCtrl gSpeedCtrl;
173 //simple mirror url admin
174 //CMirrorAdmin gMirrorAdmin;
175
176