Example #1
0
/**
 * gva_main_init_search_completion:
 * @error: return location for a #GError, or %NULL
 *
 * Initializes autocompletion in the search entry.  This must be done
 * <emphasis>after</emphasis> the game database is built and ROMs are
 * analyzed.  If an error occurs, the function returns %FALSE and sets
 * @error.
 *
 * Returns: %TRUE if autocompletion was initialized successfully,
 *          %FALSE if an error occurred
 **/
gboolean
gva_main_init_search_completion (GError **error)
{
        GtkEntryCompletion *completion;
        GtkCellRenderer *renderer;
        GtkListStore *store;
        GtkTreeIter iter;
        GtkEntry *entry;
        sqlite3_stmt *stmt;
        gint errcode;

        GList *list;

        if (!gva_db_prepare (SQL_COMPLETION_LIST, &stmt, error))
                return FALSE;

        store = gtk_list_store_new (
                4, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);

        while ((errcode = sqlite3_step (stmt)) == SQLITE_ROW)
        {
                GvaGameStoreColumn column_id;
                const gchar *column_name;
                const gchar *column_title;
                const gchar *search_text;
                gchar *collation_key;

                search_text = (const gchar *) sqlite3_column_text (stmt, 0);
                column_name = (const gchar *) sqlite3_column_text (stmt, 1);
                gva_columns_lookup_id (column_name, &column_id);
                column_title = gva_columns_lookup_title (column_id);

                if (search_text == NULL || *search_text == '\0')
                        continue;

                gtk_list_store_append (store, &iter);
                collation_key = gva_search_collate_key (search_text);
                gtk_list_store_set (
                        store, &iter,
                        COLUMN_NAME, column_name,
                        COLUMN_TEXT, search_text,
                        COLUMN_TYPE, column_title,
                        COLUMN_CKEY, collation_key, -1);
                g_free (collation_key);
        }

        sqlite3_finalize (stmt);

        if (errcode != SQLITE_DONE)
        {
                gva_db_set_error (error, 0, NULL);
                g_object_unref (store);
                return FALSE;
        }

        completion = gtk_entry_completion_new ();
        gtk_entry_completion_set_match_func (
                completion, (GtkEntryCompletionMatchFunc)
                main_entry_completion_match, NULL, NULL);
        gtk_entry_completion_set_minimum_key_length (completion, 3);
        gtk_entry_completion_set_model (completion, GTK_TREE_MODEL (store));
        gtk_entry_completion_set_text_column (completion, COLUMN_TEXT);

        g_signal_connect (
                completion, "match-selected",
                G_CALLBACK (main_entry_completion_match_selected_cb), NULL);

        list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (completion));
        g_object_set (list->data, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
        g_list_free (list);

        renderer = gtk_cell_renderer_text_new ();
        g_object_set (renderer, "sensitive", FALSE, "xalign", 1.0, NULL);
        gtk_cell_layout_pack_start (
                GTK_CELL_LAYOUT (completion), renderer, FALSE);
        gtk_cell_layout_add_attribute (
                GTK_CELL_LAYOUT (completion), renderer, "text", COLUMN_TYPE);

        entry = GTK_ENTRY (GVA_WIDGET_MAIN_SEARCH_ENTRY);
        gtk_entry_set_completion (entry, completion);

        return TRUE;
}
/**
 * gva_play_back_window_hide_cb:
 * @window: the "Recorded Games" window
 *
 * Handler for #GtkWidget::hide signals to the "Recorded Games" window.
 *
 * Saves the contents of the "Recorded Games" tree view to the game database.
 **/
void
gva_play_back_window_hide_cb (GtkWindow *window)
{
        GtkTreeView *view;
        GtkTreeModel *model;
        GtkTreeIter iter;
        sqlite3_stmt *stmt;
        gboolean valid;
        GError *error = NULL;

        view = GTK_TREE_VIEW (GVA_WIDGET_PLAY_BACK_TREE_VIEW);
        model = gtk_tree_view_get_model (view);
        valid = gtk_tree_model_get_iter_first (model, &iter);

        if (!gva_db_transaction_begin (&error))
                goto exit;

        if (!gva_db_execute (SQL_DELETE_PLAY_BACK, &error))
                goto rollback;

        if (!gva_db_prepare (SQL_INSERT_PLAY_BACK, &stmt, &error))
                goto rollback;

        while (valid)
        {
                gchar *comment;
                gchar *name;
                gchar *utf8;
                gint64 inode;
                gint index;
                gint errcode;

                gtk_tree_model_get (
                        model, &iter,
                        GVA_GAME_STORE_COLUMN_NAME, &name,
                        GVA_GAME_STORE_COLUMN_INODE, &inode,
                        GVA_GAME_STORE_COLUMN_COMMENT, &comment,
                        -1);

                index = sqlite3_bind_parameter_index (stmt, "@name");
                utf8 = g_locale_to_utf8 (name, -1, NULL, NULL, &error);
                gva_error_handle (&error);

                if (utf8 == NULL)
                {
                        g_free (name);
                        g_free (comment);
                        sqlite3_finalize (stmt);
                        goto rollback;
                }

                errcode = sqlite3_bind_text (stmt, index, utf8, -1, g_free);

                if (errcode != SQLITE_OK)
                {
                        g_free (name);
                        g_free (comment);
                        sqlite3_finalize (stmt);
                        goto rollback;
                }

                index = sqlite3_bind_parameter_index (stmt, "@inode");
                errcode = sqlite3_bind_int64 (stmt, index, inode);

                if (errcode != SQLITE_OK)
                {
                        g_free (name);
                        g_free (comment);
                        sqlite3_finalize (stmt);
                        goto rollback;
                }

                index = sqlite3_bind_parameter_index (stmt, "@comment");
                utf8 = g_locale_to_utf8 (comment, -1, NULL, NULL, &error);
                gva_error_handle (&error);

                if (utf8 == NULL)
                {
                        g_free (name);
                        g_free (comment);
                        sqlite3_finalize (stmt);
                        goto rollback;
                }

                errcode = sqlite3_bind_text (stmt, index, utf8, -1, g_free);

                if (errcode != SQLITE_OK)
                {
                        g_free (name);
                        g_free (comment);
                        sqlite3_finalize (stmt);
                        goto rollback;
                }

                g_free (name);
                g_free (comment);

                if (sqlite3_step (stmt) != SQLITE_DONE)
                {
                        gva_db_set_error (&error, 0, NULL);
                        gva_error_handle (&error);
                        sqlite3_finalize (stmt);
                        goto rollback;
                }

                sqlite3_reset (stmt);
                sqlite3_clear_bindings (stmt);

                valid = gtk_tree_model_iter_next (model, &iter);
        }

        sqlite3_finalize (stmt);

        if (!gva_db_transaction_commit (&error))
                goto rollback;

        goto exit;

rollback:
        gva_error_handle (&error);
        gva_db_transaction_rollback (&error);

exit:
        gva_error_handle (&error);
}
Example #3
0
static void
wnck_window_initialize (WnckWindow *window)
{
        const gchar *game;
        sqlite3_stmt *stmt;
        gchar *sql;
        gint errcode;
        gboolean success;
        GError *error = NULL;

        game = wnck_window_get_game (window);

        sql = g_strdup_printf (SQL_SELECT_GAME_WINDOW, game);
        success = gva_db_prepare (sql, &stmt, &error);
        gva_error_handle (&error);
        g_free (sql);

        if (!success)
                return;

        errcode = sqlite3_step (stmt);

        /* Restore the window's previous geometry. */
        if (errcode == SQLITE_ROW)
        {
                gint x, y;
                gint width, height;
                gboolean maximized;

                x = sqlite3_column_int (stmt, COLUMN_X);
                y = sqlite3_column_int (stmt, COLUMN_Y);
                width = sqlite3_column_int (stmt, COLUMN_WIDTH);
                height = sqlite3_column_int (stmt, COLUMN_HEIGHT);
                maximized = sqlite3_column_int (stmt, COLUMN_MAXIMIZED);

                wnck_window_set_geometry (
                        window, WNCK_WINDOW_GRAVITY_CURRENT,
                        WNCK_WINDOW_CHANGE_X | WNCK_WINDOW_CHANGE_Y |
                        WNCK_WINDOW_CHANGE_WIDTH | WNCK_WINDOW_CHANGE_HEIGHT,
                        x, y, width, height);

                if (maximized)
                        wnck_window_maximize (window);
                else
                        wnck_window_unmaximize (window);
        }

        /* Create a new record using the current geometry. */
        else if (errcode == SQLITE_DONE)
        {
                gint x, y;
                gint width, height;
                gboolean maximized;

                maximized = wnck_window_is_maximized (window);
                wnck_window_get_geometry (window, &x, &y, &width, &height);

                sql = g_strdup_printf (
                        "INSERT INTO window VALUES "
                        "('%s', %d, %d, %d, %d, %d)",
                        game, x, y, width, height, maximized);
                gva_db_execute (sql, &error);
                gva_error_handle (&error);
                g_free (sql);
        }

        /* Something went wrong. */
        else
        {
                gva_db_set_error (&error, 0, NULL);
                gva_error_handle (&error);
        }

        sqlite3_finalize (stmt);
}