/// <summary>
 /// Loads the features listed in the 'features' property.
 /// The properties should be name value pairs separated by "=", without any spaces around separator.
 /// The value for property is always cast to string.
 /// </summary>
 void LoadFeatures()
 {
     try
     {
         StringReader reader = new StringReader(Mowbly.GetProperty <string>(Constants.PROPERTY_FEATURES));
         if (reader != null)
         {
             string line;
             while ((line = reader.ReadLine()) != null)
             {
                 // Read name=value pair. Ignore comments (#) and empty lines.
                 if (!line.StartsWith("#") && !line.Equals(String.Empty))
                 {
                     line = line.Trim();
                     string[] property = line.Split('=');
                     features.Add(property[0], string.Join("=", property.Skip(1).ToArray()));
                     //Logger.Warn("Key: " + property[0] + "Value" + string.Join("=", property.Skip(1).ToArray()));
                 }
             }
         }
         else
         {
             Logger.Error("Could not load features. Reason - Error reading features property");
         }
     }
     catch (Exception e)
     {
         Logger.Error("Could not load features. Reason - " + e.Message);
     }
 }
Esempio n. 2
0
        LoggerManager()
        {
            // Initialize Loggers
            ConsoleLogHandler  cHandler = new ConsoleLogHandler();
            FileLogHandler     fHandler = new FileLogHandler(Mowbly.LogFile);
            DatabaseLogHandler dHandler = new DatabaseLogHandler(
                Mowbly.GetProperty <string>(Constants.PROPERTY_LOGS_DB),
                float.Parse(Mowbly.GetProperty <string>(Constants.PROPERTY_LOGS_DB_VERSION)),
                Mowbly.LogDatabaseFile,
                System.String.Empty);

            // System Logger
            Logger systemLogger = GetLogger(TYPE_SYSTEM_LOGGER);

            systemLogger.AddHandler(fHandler);
            systemLogger.AddHandler(dHandler);

            // User Logger
            Logger userLogger = GetLogger(TYPE_USER_LOGGER);

            userLogger.AddHandler(fHandler);
            userLogger.AddHandler(dHandler);

#if DEBUG
            systemLogger.AddHandler(cHandler);
            userLogger.AddHandler(cHandler);
#endif
        }
Esempio n. 3
0
        void OnNavigationFailed(object sender, NavigationFailedEventArgs e)
        {
            Logger.Error("Navigation failed for url " + e.Uri.ToString());

            isNavigationFailed = true;

            // Raise onLoadFailed event
            RaisePageEvent(OnLoadFailed, new PageEventArgs());
            try
            {
                // Navigate to page not found
                string pnfUrl = Mowbly.GetProperty <string>(Constants.PROPERTY_URL_PAGE_NOT_FOUND);
                browser.Navigate(new Uri(pnfUrl, UriKind.Relative));
            }
            catch (Exception ex)
            {
                Logger.Error("Error navigating to 404 page. Reason - " + ex.Message);
            }
            e.Handled = true;
        }
        // Invoked when device back key pressed
        public void BackKeyPress(CancelEventArgs e)
        {
            // Notify event handlers
            if (OnBackKeyPress != null)
            {
                OnBackKeyPress(null, e);
            }

            // If none has acted, proceed closing the active page
            if (!e.Cancel)
            {
                PageModel activePage = PageManager.Instance.ActivePage;
                if (activePage != null)
                {
                    if (!activePage.Name.Equals(Mowbly.GetProperty <string>(Constants.PROPERTY_TITLE_HOME_PAGE)))
                    {
                        activePage.Close(true);
                        e.Cancel = true;
                    }
                }
            }
        }
        /// <summary>
        /// Invoke the method specified by the
        /// <see cref="CloudPact.MowblyFramework.Core.Features.JSMessage">JSMessage</see> object
        /// </summary>
        /// <param name="message">
        /// <see cref="CloudPact.MowblyFramework.Core.Features.JSMessage">JSMessage</see> object
        /// </param>
        internal async override void InvokeAsync(JSMessage message)
        {
            string callbackId = message.CallbackId;

            try
            {
                // Variables
                SqliteConnection connection;
                DBConfig         dbConfig;
                DbTransaction    transaction;
                string           connectionId, query, queryId;
                long             dbSize;
                JObject          o;

                switch (message.Method)
                {
                case "openDatabase":

                    string    dbName     = message.Args[0] as string;
                    Int64     dblevel    = (Int64)message.Args[1];
                    FileLevel level      = (FileLevel)dblevel;
                    Int64     version    = (Int64)message.Args[2];
                    float     dbVersion  = (float)version;
                    string    dbPassword = message.Args[3] as string;
                    string    dbPath     = null;
                    if (dbName.Equals(Mowbly.GetProperty <string>(Constants.PROPERTY_LOGS_DB)))
                    {
                        dbPath = Mowbly.LogDatabaseFile;
                        if (!FileManager.FileExists(dbPath))
                        {
                            FileManager.CreateFile(dbPath);
                        }
                    }
                    else
                    {
                        dbPath = FileManager.GetAbsolutePath(new FilePath
                        {
                            Path        = Path.Combine(Constants.DIR_DB, String.Concat(dbName, dbVersion.ToString())),
                            Level       = level,
                            StorageType = StorageType.Internal
                        });
                    }

                    dbSize = FileManager.GetFileSize(dbPath);

                    // Create new connection
                    try
                    {
                        connection = new SqliteConnection();
                        connection.ConnectionString = DBUtils.GetConnectionString(dbPath, dbPassword);
                        connection.Open();
                        connectionId = Guid.NewGuid().ToString();
                        dbConfigDict.Add(connectionId, new DBConfig
                        {
                            DBName     = dbName,
                            DBVersion  = dbVersion,
                            DBPath     = dbPath,
                            DBPassword = dbPassword,
                            Connection = connection
                        });

                        InvokeCallbackJavascript(callbackId, new MethodResult {
                            Result = connectionId
                        });
                    }
                    catch (SqliteException se)
                    {
                        string error = String.Concat(Mowbly.GetString(Constants.STRING_DATABASE_OPEN_ERROR), se.Message);
                        Logger.Error(error);
                        JToken  opt = message.Args[0] as JToken;
                        string  Id  = opt["queryId"].ToObject <string>();
                        JObject obj = new JObject();
                        obj.Add("queryId", Id);
                        InvokeCallbackJavascript(callbackId, new MethodResult
                        {
                            Code   = MethodResult.FAILURE_CODE,
                            Result = obj,
                            Error  = new MethodError
                            {
                                Message = error
                            }
                        });
                    }

                    break;

                case "executeQuery":

                    // Get connection id
                    JToken options = message.Args[0] as JToken;
                    connectionId = options["id"].ToObject <string>();
                    dbConfig     = dbConfigDict[connectionId];

                    // Read args
                    queryId = options["queryId"].ToObject <string>();
                    query   = options["sql"].ToObject <string>().Trim();
                    List <object> queryParams = options["params"].ToObject <List <object> >();

                    // Execute query
                    try
                    {
                        JArray data         = null;
                        int    rowsAffected = 0;
                        connection = dbConfig.Connection;

                        // Throw exception is connection is null
                        if (connection == null)
                        {
                            throw new ArgumentException(Mowbly.GetString(Constants.STRING_DATABASE_NO_CONNECTION_OPEN_ERROR));
                        }

                        // Execute query
                        if (query.ToLower().StartsWith("select"))
                        {
                            data = ProcessSelectQuery(ref connection, query, queryParams);
                        }
                        else
                        {
                            rowsAffected = ProcessNonQuery(ref connection, query, queryParams);
                        }

                        // Create result
                        o = new JObject();
                        o.Add("queryId", queryId);

                        JObject d = new JObject();
                        d.Add("rowsAffected", rowsAffected);
                        d.Add("insertId", connection.LastInsertRowId);
                        d.Add("rows", data);
                        o.Add("data", d);

                        // Notify JS
                        InvokeCallbackJavascript(callbackId, new MethodResult {
                            Result = o
                        });
                    }
                    catch (SqliteException se)
                    {
                        // Error
                        string error = String.Concat(Mowbly.GetString(Constants.STRING_DATABASE_QUERY_ERROR), se.Message);
                        Logger.Error(error);

                        // Create result
                        o = new JObject();
                        o.Add("queryId", queryId);

                        // Notify Js
                        InvokeCallbackJavascript(callbackId, new MethodResult
                        {
                            Code   = MethodResult.FAILURE_CODE,
                            Result = o,
                            Error  = new MethodError
                            {
                                Message = error
                            }
                        });
                    }

                    break;

                case "beginTransaction":

                    connectionId = message.Args[0] as string;
                    dbConfig     = dbConfigDict[connectionId];

                    // Begin transaction
                    try
                    {
                        connection = dbConfig.Connection;
                        // Throw exception is connection is null
                        if (connection == null)
                        {
                            throw new ArgumentException(Mowbly.GetString(Constants.STRING_DATABASE_NO_CONNECTION_OPEN_ERROR));
                        }

                        transaction          = connection.BeginTransaction();
                        dbConfig.Transaction = transaction;

                        // Notify JS
                        InvokeCallbackJavascript(callbackId, new MethodResult {
                            Result = true
                        });
                    }
                    catch (SqliteException se)
                    {
                        // Error; Notify JS
                        string error = String.Concat(Mowbly.GetString(Constants.STRING_DATABASE_TRANSACTION_ERROR), se.Message);
                        Logger.Error(error);
                        JToken  opt = message.Args[0] as JToken;
                        string  Id  = opt["queryId"].ToObject <string>();
                        JObject obj = new JObject();
                        obj.Add("queryId", Id);
                        InvokeCallbackJavascript(callbackId, new MethodResult
                        {
                            Code   = MethodResult.FAILURE_CODE,
                            Result = obj,
                            Error  = new MethodError
                            {
                                Message = error
                            }
                        });
                    }

                    break;

                case "commit":

                    connectionId = message.Args[0] as string;
                    dbConfig     = dbConfigDict[connectionId];

                    // Commit transaction
                    try
                    {
                        transaction = dbConfig.Transaction;
                        // Throw exception is transaction is null
                        if (transaction == null)
                        {
                            throw new ArgumentException(Mowbly.GetString(Constants.STRING_DATABASE_NO_TRANSACTION_ACTIVE_ERROR));
                        }
                        transaction.Commit();

                        queryId      = message.Args[1] as string;
                        o            = new JObject();
                        o["queryId"] = queryId;
                        o["data"]    = true;

                        // Notify JS
                        InvokeCallbackJavascript(callbackId, new MethodResult {
                            Result = o
                        });
                    }
                    catch (SqliteException se)
                    {
                        // Error; Notify JS
                        string error = String.Concat(Mowbly.GetString(Constants.STRING_DATABASE_TRANSACTION_ERROR), se.Message);
                        Logger.Error(error);
                        JToken  opt = message.Args[0] as JToken;
                        string  Id  = opt["queryId"].ToObject <string>();
                        JObject obj = new JObject();
                        obj.Add("queryId", Id);
                        InvokeCallbackJavascript(callbackId, new MethodResult
                        {
                            Code   = MethodResult.FAILURE_CODE,
                            Result = obj,
                            Error  = new MethodError
                            {
                                Message = error
                            }
                        });
                    }

                    break;

                case "rollback":

                    connectionId = message.Args[0] as string;
                    dbConfig     = dbConfigDict[connectionId];

                    // Commit transaction
                    try
                    {
                        transaction = dbConfig.Transaction;
                        transaction.Rollback();

                        queryId      = message.Args[1] as string;
                        o            = new JObject();
                        o["queryId"] = queryId;
                        o["data"]    = true;

                        // Notify JS
                        InvokeCallbackJavascript(callbackId, new MethodResult {
                            Result = o
                        });
                    }
                    catch (SqliteException se)
                    {
                        // Error; Notify JS
                        string error = String.Concat(Mowbly.GetString(Constants.STRING_DATABASE_TRANSACTION_ERROR), se.Message);
                        Logger.Error(error);
                        JToken  opt = message.Args[0] as JToken;
                        string  Id  = opt["queryId"].ToObject <string>();
                        JObject obj = new JObject();
                        obj.Add("queryId", Id);
                        InvokeCallbackJavascript(callbackId, new MethodResult
                        {
                            Code   = MethodResult.FAILURE_CODE,
                            Result = obj,
                            Error  = new MethodError
                            {
                                Message = error
                            }
                        });
                    }

                    break;

                default:
                    Logger.Error("Feature " + Name + " does not support method " + message.Method);
                    break;
                }
            }
            catch (Exception e)
            {
                // Error; Notify JS
                string error = String.Concat(Mowbly.GetString(Constants.STRING_DATABASE_OPERATION_ERROR), e.Message);
                Logger.Error(error);
                JToken  opt = message.Args[0] as JToken;
                string  Id  = opt["queryId"].ToObject <string>();
                JObject obj = new JObject();
                obj.Add("queryId", Id);
                InvokeCallbackJavascript(callbackId, new MethodResult
                {
                    Code   = MethodResult.FAILURE_CODE,
                    Result = obj,
                    Error  = new MethodError
                    {
                        Message = error
                    }
                });
            }
            await Task.FromResult(0);
        }
 void LaunchHomePage()
 {
     LaunchPage(Mowbly.GetProperty <string>(Constants.PROPERTY_TITLE_HOME_PAGE),
                Mowbly.GetProperty <string>(Constants.PROPERTY_URL_HOME_PAGE));
 }