diff --git a/src/main.js b/src/main.js index 9d1788e..6cb1bd2 100755 --- a/src/main.js +++ b/src/main.js @@ -60,19 +60,12 @@ var App = { }, onLoad: function() { // Initializes the global variables - var split = Verdnatura.commandLine.match(/(?:[^\s"]+|"[^"]*")+/g); - - if (split.length > 1) - this.module = split[1].replace(/^"+|"+$/g, ''); - if (!this.module) - this.module = Conf.defaultModule; - - this.appDir = this.getEnv('ProgramFiles') + '\\' + Conf.appName; - this.moduleDir = this.shell.SpecialFolders('AppData') + '\\' + Conf.appName; - this.compressFile = this.getEnv('TEMP') + '\\' + this.module + '.7z'; - this.mdbFile = this.moduleDir + '\\' + this.module + '.mdb'; - this.lockFile = this.moduleDir + '\\' + this.module + '.ldb'; - this.certFile = this.appDir + '\\cacert.pem'; + this.module = App.getModule(); + this.appDir = this.getEnv('ProgramFiles') +'\\'+ Conf.appName; + this.moduleDir = this.shell.SpecialFolders('AppData') +'\\'+ Conf.appName; + this.mdbPath = this.moduleDir +'\\' + this.module; + this.compressFile = this.getEnv('TEMP') +'\\'+ this.module +'.7z'; + this.certFile = this.appDir +'\\cacert.pem'; // Creates the necessary registry entries var lastVersion = this.regRead(Conf.regPath, 'lastExecutedVersion'); @@ -127,7 +120,7 @@ var App = { var selectDatarouce = document.querySelector('#datasource'); var option = []; var odbcName; - var allOdbc = this.EnumValues('HKCU\\SOFTWARE\\ODBC\\ODBC.INI\\ODBC Data Sources\\') + var allOdbc = this.enumValues(Conf.odbcPath +'ODBC Data Sources\\') for (var y in allOdbc) { if (allOdbc[y].slice(-3) == Conf.identifier) { odbcName = allOdbc[y].replace(Conf.identifier, '') @@ -368,28 +361,11 @@ var App = { mysqlConn.close(); // Check the cretentials and return the last version number - var version = this.fetchVersion(); - - // Check if there is a new version, and if there is, download it - if (version) { - this.disableUi(true, _('Updating')); - var remoteFile = version - ? '.archive/'+ this.module +'/'+ version +'.7z' - : this.module +'.7z?'+ new Date().getTime(); - remoteFile = Conf.cdnURL +'/'+ remoteFile; - var request = new ActiveXObject('MSXML2.XMLHTTP.6.0'); - request.open('GET', remoteFile, true); - request.onreadystatechange = function() { - App.onRequestReady(request); - }; - request.send(); - } else - this.openMdb(); + this.fetchVersion(); } catch (err) { this.catchError(err); } }, - /** * Gets information about the version to download. * cmdle @@ -410,49 +386,68 @@ var App = { this.request('GET', 'MdbVersions/findOne', params, function(err, res) { if (err) throw new Error ('Version could not be retrieved: '+ err.message +': '); - alert(this.lockFile) + // Checks if it's already open - if (App.fso.fileExists(this.lockFile)) + if (App.fso.fileExists(App.lockFile())) try { - App.fso.deleteFile(this.lockFile); - return res.version; + App.fso.deleteFile(App.lockFile()); + this.version = res.version; } catch (e) { throw new Error(_('Application it\'s already open')); } // Checks if MDB exists - if (!App.fso.fileExists(this.mdbFile)) - return res.version; + if (!App.fso.fileExists(App.mdbFile())) + this.version = res.version; // If it's abnormaly bigger, maybe is corrupted, so force download - var file = App.fso.getFile(this.mdbFile); + if (!this.version) { + var file = App.fso.getFile(App.mdbFile()); + if (file.size > Conf.maxCorruptSize * 1024 * 1024) + this.version = res.version; + + // Obtains the local version number from the MDB file + var localVersion = App.mdbGetValue( + 'SELECT Version FROM tblVariables', + 'Version', parseInt + ); + if (!localVersion) + localVersion = false; + // Determines if should download + !localVersion || res.version === false || localVersion != res.version + ? this.version = res.version + : this.version = null; + } - if (file.size > Conf.maxCorruptSize * 1024 * 1024) - return res.version; - - // Obtains the local version number from the MDB file - var localVersion = this.mdbGetValue( - 'SELECT Version FROM tblVariables', - 'Version', parseInt - ); - if (!localVersion) - localVersion = false; - - // Determines if should download - return !localVersion || res.version === false || localVersion != res.version - ? res.version - : null; + if (this.version) { + // Check if there is a new version, and if there is, download it + if (version) { + App.disableUi(true, _('Updating')); + var module = App.getModule(); + var remoteFile = this.version + ? '.archive/'+ module +'/'+ this.version +'.7z' + : module +'.7z?'+ new Date().getTime(); + remoteFile = Conf.cdnURL +'/'+ remoteFile; + var request = new ActiveXObject('MSXML2.XMLHTTP.6.0'); + request.open('GET', remoteFile, true); + request.onreadystatechange = function() { + App.onRequestReady(request); + }; + request.send(); + } else + this.openMdb(); + } }); }, mdbGetValue: function(query, field, parseFn) { var value; try { - if (this.fso.fileExists(this.mdbFile)) { + if (this.fso.fileExists(this.mdbFile())) { var mdbConn = new ActiveXObject('ADODB.Connection'); mdbConn.open(this.getODBCString({ 'Provider': 'Microsoft.Jet.OLEDB.4.0', - 'Data Source': this.mdbFile + 'Data Source': this.mdbFile() })); try { @@ -493,8 +488,8 @@ var App = { stream.saveToFile(this.compressFile, 2); stream.close(); - if (this.fso.fileExists(this.mdbFile)) - this.fso.deleteFile(this.mdbFile); + if (this.fso.fileExists(this.mdbFile())) + this.fso.deleteFile(this.mdbFile()); this.run('7za e "'+ this.compressFile +'" -o"'+ this.moduleDir +'"', true); this.fso.deleteFile(this.compressFile); @@ -503,7 +498,7 @@ var App = { } try { - if (!this.fso.fileExists(this.mdbFile)) + if (!this.fso.fileExists(this.mdbFile())) throw new Error(_('MDB file not found')); this.openMdb(); @@ -521,7 +516,7 @@ var App = { if (!this.fso.fileExists(accessBin)) throw new Error(_('Microsoft Access 2003 is not installed')); - this.shell.exec('"'+ accessBin +'" "'+ this.mdbFile +'"'); + this.shell.exec('"'+ accessBin +'" "'+ this.mdbFile() +'"'); window.close(); }, catchError: function(err) { @@ -717,7 +712,7 @@ var App = { Conf.identifier return odbcPath; }, - EnumValues: function(RegKey) { + enumValues: function(RegKey) { var RootKey = new Object() RootKey["HKCR"] = RootKey["HKEY_CLASSES_ROOT"] = 0x80000000; RootKey["HKCU"] = RootKey["HKEY_CURRENT_USER"] = 0x80000001; @@ -729,14 +724,27 @@ var App = { Locator = new ActiveXObject("WbemScripting.SWbemLocator"); ServerConn = Locator.ConnectServer(null, "root\\default"); Registry = ServerConn.Get("StdRegProv"); - Method = Registry.Methods_.Item("EnumValues"); + Method = Registry.Methods_.Item("enumValues"); p_In = Method.InParameters.SpawnInstance_(); p_In.hDefKey = RootVal; p_In.sSubKeyName = RegKey.substr(RegKey.indexOf("\\") + 1) p_Out = Registry.ExecMethod_(Method.Name, p_In); return p_Out.sNames.toArray(); } - } + }, + getModule: function() { + var split = Verdnatura.commandLine.match(/(?:[^\s"]+|"[^"]*")+/g); + if (split.length > 1) + return split[1].replace(/^"+|"+$/g, ''); + if (!this.module) + return Conf.defaultModule; + }, + lockFile: function() { + return this.mdbPath +'.ldb'; + }, + mdbFile: function() { + return this.mdbPath +'.mdb'; + } }; App.init();