使用soui开发的mbc,只支持windows版本
w1146869587
2022-01-24 479b1995ef435713c2cf4f0da8de3a6af6c30922
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
// DirectoryChangeMgr.cpp : implementation file
//
 
#include "stdafx.h"
#include "OIMOBOXWatchDir.h"
#include "DirectoryChangeMgr.h"
#include "HighTime.h"
 
// CDirectoryChangeMgr
 
CDirectoryChangeMgr::CDirectoryChangeMgr()
{
    m_strLogin        = _T("");
    m_strServerID    = _T("");
    m_strDBFile        = _T("");
    m_bOpenDB        = FALSE;    
    m_arBackupDir.RemoveAll();
}
 
CDirectoryChangeMgr::~CDirectoryChangeMgr()
{
        CloseDB( );
}
// CDirectoryChangeMgr member functions
BOOL CDirectoryChangeMgr::OpenDB( CString strLogin, CString strPassword, CString strServerID, BOOL bCheckFile )
{
    if ( strLogin.IsEmpty() || strServerID.IsEmpty() )
        return    FALSE;
    CloseDB();
    CString        strPathName, strFileName;
    //CString        strAccessPathName;
    CString        strTemplateDBFile;
    CString        strDBPassword;
    BOOL        bExistFile = FALSE;
 
    strPathName    = GetNetDiskDBWorkDir() ;
    if ( !ExistPath( strPathName ) )
    {
        if ( !::CreateDirectory( strPathName, NULL ) )
            return    FALSE;
    }
    strPathName        = strPathName + _T( "\\" ) + strLogin;
    if ( !ExistPath( strPathName ) )
    {
        if ( !::CreateDirectory( strPathName , NULL ) )
            return    FALSE;
    }
    strFileName.Format( _T("%s\\%s_mb_watch.db"), strPathName, strServerID );
    bExistFile = ExistFile( strFileName );
    if ( bCheckFile && !bExistFile )
        return    FALSE;
 
    strTemplateDBFile = GetTemplateDBFile( );
    if ( strTemplateDBFile.IsEmpty() )
        return FALSE;
    if ( !bExistFile && !CopyFile( strTemplateDBFile, strFileName, FALSE ) )
    {
        return FALSE;
    }    
    if ( !m_AConnect.Open( strFileName ,strPassword ) )
        return FALSE;
    m_strDBFile    = strFileName;
    m_bOpenDB    = TRUE;
    return    TRUE;
}
 
void CDirectoryChangeMgr::CloseDB()
{
    if ( m_bOpenDB )
    {
        m_AConnect.Close();
        m_bOpenDB    = FALSE;
        m_strDBFile    = _T( "" );
    }
}
 
CString    CDirectoryChangeMgr::GetTemplateDBFile( )
{
    CString        strPathName;
    CString        strTemplateDBFile;
 
    strPathName        = GetModulePath() + _T( "\\Data" );
    if ( !ExistPath( strPathName ) )
    {
        if ( !::CreateDirectory( strPathName, NULL ) )
            return    _T("");
    }
    strTemplateDBFile = strPathName + _T("\\mb_watch.db");
    return strTemplateDBFile;
}
BOOL CDirectoryChangeMgr::InsertChangeDir(CString strFilePath, CString strOperateType, CString strFileParam )
{
    CString strBackupDir = _T("");
    CString strDir;
    CString strTempPath;
    BOOL        bReturn;
    if(!m_bOpenDB)
        return FALSE;
 
    strTempPath = strFilePath;
    strTempPath.MakeLower();
    for(int i = 0; i < m_arBackupDir.GetCount(); i++)
    {
        strDir = m_arBackupDir.GetAt(i);
        strDir.MakeLower();
        strDir +=_T("\\");
        if(strTempPath.Find(strDir) == 0)
        {
            strBackupDir =strDir;
            break;
        }
    }
    CString            strSQL;
    CHighTime dtCreate = CHighTime::GetPresentTime();
    strSQL.Format(_T("INSERT INTO TN_ND_BACKUPDIR_FILE (CN_G_ID,CN_S_FILE_PATH,CN_S_OP_TYPE,CN_S_PARAM_VALUE,CN_T_CREATE) \
                     VALUES ('%s','%s','%s','%s','%s')"), 
                     GenerateGUIDString(),GetDBReplaceStr( strFilePath),strOperateType,GetDBReplaceStr( strFileParam),dtCreate.Format(_T("%Y-%m-%d %H:%M:%S")));
    try
    {
        bReturn = CDBSQLiteCommFun::RunSQL( strSQL, &m_AConnect);
    }
    catch ( CDBSQliteException ex )
    {
        CString        strError = ex.GetErrInfo();
        CString        strErrInfo;
        strErrInfo = _T("CDirectoryChangeMgr::InsertChangeDir - ") + strError;
        WriteDBErrToFile( strErrInfo );
        return FALSE;
    }
    return TRUE;
}
 
CString CDirectoryChangeMgr::GetNetDiskDBWorkDir()
{
    return GetModulePath() +_T("\\Users");
    CString        strWorkDir;
    CRegKey        reg;
    CString        strSubKey;
    TCHAR        szValue[256]    = { 0 };
    DWORD        dwReaded;
 
    strSubKey.Format( _T("%s"), REG_DBSUBKEY );
    if ( ERROR_SUCCESS == reg.Open( HKEY_LOCAL_MACHINE, strSubKey, KEY_READ ) )
    {
        dwReaded    = 255;
        if ( ERROR_SUCCESS == reg.QueryValue( szValue, _T( "WorkDir" ), &dwReaded ) && dwReaded > 0 )
        {
            strWorkDir     = szValue;
            strWorkDir.TrimRight();
            strWorkDir.TrimRight( '\\' );
        }
 
        reg.Close();
    }
    if ( strWorkDir.IsEmpty() )
        strWorkDir = GetModulePath( );
 
    return    strWorkDir;
}
CString CDirectoryChangeMgr::GetModulePath()
{
    CString        strFileName;
 
    GetModuleFileName( NULL, strFileName.GetBuffer( MAX_PATH ), MAX_PATH );
    strFileName.ReleaseBuffer();
 
    return    GetPathName( strFileName );
}
 
CString CDirectoryChangeMgr::GetPathName( CString strFileName )
{
    int            nPos;
    CString        strPathName;
 
    if ( strFileName.IsEmpty() )
        return    _T( "" );
 
    nPos    = strFileName.ReverseFind( '\\' );
    if ( nPos < 0 )
        return    _T( "" );
 
    strPathName    = strFileName.Left( nPos );
    return    strPathName;
}
BOOL CDirectoryChangeMgr::ExistPath( CString strFilePath )
{
    DWORD        dwAttr;
 
 
    if ( strFilePath.IsEmpty() )
        return    FALSE;
    dwAttr = ::GetFileAttributes(strFilePath);
    if(dwAttr != -1 && dwAttr & FILE_ATTRIBUTE_DIRECTORY )
        return TRUE;
    return    FALSE;
}
 
BOOL CDirectoryChangeMgr::ExistFile( CString strFileName )
{
    DWORD        dwAttr;
 
 
    if ( strFileName.IsEmpty() )
        return    FALSE;
    dwAttr = ::GetFileAttributes(strFileName);
    if(dwAttr != -1 && !(dwAttr & FILE_ATTRIBUTE_DIRECTORY))
        return TRUE;
    return    FALSE;
}
 
CString CDirectoryChangeMgr::GenerateGUIDString()
{
    CString strGUID;
    GUID    guid;
 
    if(CoCreateGuid( &guid ) == S_OK)
    {
        OLECHAR theGUID[MAX_PATH];
        StringFromGUID2(guid, theGUID, MAX_PATH);
        strGUID = theGUID;
    }
 
    return strGUID;
}
 
BOOL CDirectoryChangeMgr::GetBackupDir(CStringArray &arBackupDir )
{
    if ( !m_bOpenDB )
        return    FALSE;
    CString            strPropClass;
    CString            strSQL;
    CDBRecord        ARecordset;
    BOOL            bReturn = FALSE;
    CString            strPropValue;
 
    arBackupDir.RemoveAll();
    strPropClass = _T("_NDBACKUPDIR");
    strSQL.Format( _T("SELECT CN_S_C4 FROM TN_ND_PROPERTY WHERE CN_S_C1='%s'"),strPropClass);
    try
    {
        ARecordset.Close();
        ARecordset.Open( &m_AConnect,strSQL );
        while ( S_OK == ARecordset.MoveNext() )
        {
            ARecordset.GetValue( 1, strPropValue );
            arBackupDir.Add(strPropValue);
        }
    }
    catch ( CDBSQliteException ex )
    {
        CString        strError = ex.GetErrInfo();
        CString        strErrInfo;
        strErrInfo = _T("CDirectoryChangeMgr::GetBackupDir - ") + strError;
        WriteDBErrToFile( strErrInfo );
    }
    strPropClass = _T("_DMSBACKUPDIR");
    strSQL.Format( _T("SELECT CN_S_C4 FROM TN_ND_PROPERTY WHERE CN_S_C1='%s'"),strPropClass);
    try
    {
        ARecordset.Close();
        ARecordset.Open( &m_AConnect,strSQL );
        while ( S_OK == ARecordset.MoveNext() )
        {
            ARecordset.GetValue( 1, strPropValue );
            arBackupDir.Add(strPropValue);
        }
    }
    catch ( CDBSQliteException ex )
    {
        CString        strError = ex.GetErrInfo();
        CString        strErrInfo;
        strErrInfo = _T("CDirectoryChangeMgr::GetBackupDir - ") + strError;
        WriteDBErrToFile( strErrInfo );
    }
    m_arBackupDir.RemoveAll();
    m_arBackupDir.Copy(arBackupDir);
    return bReturn;
}
 
void CDirectoryChangeMgr::WriteDBErrToFile( CString strErrInfo )
{
    CString        strWorkDir = GetModulePath( );
    CString        strErrPath = strWorkDir + _T("\\MOBOXWatchDirErr.Log");
    CHighTime    dtCurTime  = CHighTime::GetPresentTime( true );
    CString        strInfo;
 
    strInfo.Format( _T("\r\n%s:\r\n%s"), dtCurTime.Format( _T("%Y-%m-%d %H:%M:%S") ), strErrInfo );
 
    WriteRowToFile( strErrPath, strInfo );
}
bool CDirectoryChangeMgr::CStringToAnsi( LPCTSTR pszT,  LPSTR *ppszA  )
{
    ULONG        cbAnsi, cCharacters;
 
    if ( pszT == NULL )
    {
        *ppszA= NULL;
        return false;
    }
#ifdef _UNICODE  
    cCharacters = wcslen(pszT);
    cbAnsi = ::WideCharToMultiByte( CP_ACP, 0, pszT, -1, NULL, 0, NULL, NULL );
    *ppszA = (LPSTR) CoTaskMemAlloc(cbAnsi+1);
    if ( NULL == *ppszA )
        return false;
    ZeroMemory(*ppszA,cbAnsi+1);
    // Convert to ANSI.
    if (0 == WideCharToMultiByte(CP_ACP, 0, pszT, cCharacters, *ppszA,
                  cbAnsi, NULL, NULL))
    {
        CoTaskMemFree(*ppszA);
        *ppszA = NULL;
        return false;
    }
#else
    cCharacters = strlen(pszT);
    *ppszA = (LPSTR) CoTaskMemAlloc(cCharacters+1);
    if ( NULL == *ppszA )
        return false;
    ZeroMemory(*ppszA,cCharacters+1);
    memcpy(*ppszA,pszT,cCharacters);
#endif
     return true;
}
 
void CDirectoryChangeMgr::WriteRowToFile(CString strFileName, CString strContent )
{
    CTime        tmCurrent    = CTime::GetCurrentTime();
    CString        strEnter    = _T(" \r\n ");
 
    CString        strBuff;
    LPSTR        pDest ;
    CFile        file;
 
    if(strFileName.IsEmpty())
        return;
    if ( !file.Open(strFileName, CFile::modeCreate | CFile::modeWrite | CFile::modeNoTruncate) )
    {
        return ;
    }
    file.Seek( 0, CFile::end );
 
    strBuff.Format( _T("%s%s"), strContent,strEnter );
    if ( CStringToAnsi(strBuff, &pDest))
    {
        file.Write( (void*)pDest, strlen(pDest));
        CoTaskMemFree(pDest);
    }
    file.Close();
    /*CStdioFile    cfile;
    CFileException    ex;
 
    if (!cfile.Open(strFileName, CFile::modeCreate | CFile::modeWrite 
                                | CFile::modeNoTruncate, &ex))
    {
        // complain if an error happened
        // no need to delete the ex object
        TCHAR        szError[1024];
 
        ex.GetErrorMessage(szError, 1024);
        TRACE("Couldn't open source file:%s", szError);
        return;
    }
 
#ifdef _UNICODE
    if ( cfile.GetLength( ) == 0 )
    {
        WORD    unicode = 0xFEFF; //UNICODE±àÂëÎļþÍ· 
 
        cfile.Write( &unicode, 2 );
    }
#endif
 
    cfile.SeekToEnd();
    cfile.WriteString(strContent);
    cfile.Close();*/
}
CString CDirectoryChangeMgr::GetDBReplaceStr( CString strData )
{
    CString            strValue;
 
    strValue = strData;
    strValue.Replace( _T("'"), _T("''") );
    
    return strValue;
}