Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Randomise extension suggestions #264

Merged
merged 1 commit into from
Sep 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/exm-browse-page.blp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ template ExmBrowsePage : Gtk.Widget {

Gtk.SearchEntry search_entry {
hexpand: true;
placeholder-text: _("e.g. \"Blur my Shell\"");

// Hook for triggering initial search
// TODO: Re-enable this once blueprint-compiler!4 is merged
Expand Down
78 changes: 78 additions & 0 deletions src/exm-browse-page.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
/* exm-browse-page.c
*
* Copyright 2022 Matthew Jakeman <mjakeman26@outlook.co.nz>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/

#include "exm-browse-page.h"

#include "exm-search-row.h"
Expand All @@ -10,6 +30,9 @@
#include "web/model/exm-search-result.h"

#include "exm-config.h"
#include "exm-utils.h"

#include <glib/gi18n.h>

struct _ExmBrowsePage
{
Expand All @@ -19,6 +42,7 @@ struct _ExmBrowsePage
ExmImageResolver *resolver;
ExmManager *manager;

GtkStringList *suggestions;
GListModel *search_results_model;
gchar *shell_version;

Expand Down Expand Up @@ -166,6 +190,24 @@ static void
on_search_entry_realize (GtkSearchEntry *search_entry,
ExmBrowsePage *self)
{
const char *suggestion;
char *fmt;
int random_index;
int num_suggestions;

// Get random suggestion
num_suggestions = g_list_model_get_n_items (self->suggestions);
random_index = g_random_int_range (0, num_suggestions);
suggestion = gtk_string_list_get_string (self->suggestions, random_index);

// Translators:
// - '%s' is an extension e.g. Blur my Shell
// - Please use unicode quotation marks e.g. “” (not "")
fmt = g_strdup_printf (_("e.g. “%s”"), suggestion);

// Set placeholder value
g_object_set (search_entry, "placeholder-text", fmt, NULL);

// Fire off a default search
search (self, "", EXM_SEARCH_SORT_POPULARITY);
gtk_widget_grab_focus (GTK_WIDGET (search_entry));
Expand Down Expand Up @@ -235,6 +277,40 @@ exm_browse_page_class_init (ExmBrowsePageClass *klass)
gtk_widget_class_set_layout_manager_type (widget_class, GTK_TYPE_BIN_LAYOUT);
}

static void
load_suggestions (ExmBrowsePage *self)
{
char *contents;
char *contents_nul_term;
char **suggest_array;
gsize length;

contents = exm_utils_read_resource ("/com/mattjakeman/ExtensionManager/suggestions.txt", &length);
self->suggestions = gtk_string_list_new (NULL);

if (contents)
{
int iter;

contents_nul_term = g_strndup (contents, length);

// Load dynamically from resource
suggest_array = g_strsplit_set (contents_nul_term, "\n", -1);

for (iter = 0; suggest_array[iter] != NULL; iter++)
gtk_string_list_append (self->suggestions, suggest_array[iter]);

g_strfreev (suggest_array);
g_free (contents_nul_term);
g_free (contents);
}
else
{
// Hardcoded fallback suggestion
gtk_string_list_append (self->suggestions, "Blur my Shell");
}
}

static void
exm_browse_page_init (ExmBrowsePage *self)
{
Expand Down Expand Up @@ -276,4 +352,6 @@ exm_browse_page_init (ExmBrowsePage *self)
"notify::manager",
G_CALLBACK (on_bind_manager),
NULL);

load_suggestions (self);
}
20 changes: 20 additions & 0 deletions src/exm-browse-page.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
/* exm-browse-page.h
*
* Copyright 2022 Matthew Jakeman <mjakeman26@outlook.co.nz>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/

#pragma once

#include <gtk/gtk.h>
Expand Down
16 changes: 4 additions & 12 deletions src/exm-release-notes-dialog.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

#include "exm-release-notes-dialog.h"

#include "exm-utils.h"

struct _ExmReleaseNotesDialog
{
GtkWindow parent_instance;
Expand Down Expand Up @@ -75,21 +77,11 @@ exm_release_notes_dialog_init (ExmReleaseNotesDialog *self)

gtk_widget_init_template (GTK_WIDGET (self));

file = g_file_new_for_uri ("resource:///com/mattjakeman/ExtensionManager/release-notes.txt");

if (!file)
{
g_critical ("Could not read release-notes.txt: invalid file");
return;
}
contents = exm_utils_read_resource ("/com/mattjakeman/ExtensionManager/release-notes.txt", &length);

if (g_file_load_contents (file, NULL, &contents, &length, NULL, &error))
if (contents)
{
buffer = gtk_text_view_get_buffer (self->text_view);
gtk_text_buffer_set_text (GTK_TEXT_BUFFER (buffer), contents, length);
}
else
{
g_critical ("Could not read release-notes.txt: %s", error->message);
}
}
57 changes: 57 additions & 0 deletions src/exm-utils.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* exm-utils.c
*
* Copyright 2022 Matthew Jakeman <mjakeman26@outlook.co.nz>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/

#include "exm-utils.h"

char *
exm_utils_read_resource (const char *resource, gsize *length)
{
GError *error = NULL;

GFile *file;
char *path;
char *contents;

g_return_val_if_fail (length != NULL, NULL);
g_return_val_if_fail (resource != NULL, NULL);

path = g_strdup_printf ("resource://%s", resource);
file = g_file_new_for_uri (path);
g_free (path);

if (!file)
{
g_critical ("Could not read %s: invalid file\n", resource);
return NULL;
}

if (g_file_load_contents (file, NULL, &contents, length, NULL, &error))
{
g_clear_object (&file);
return contents;
}
else
{
g_critical ("Could not read %s: %s\n", resource, error->message);
}

g_clear_object (&file);
return NULL;
}
26 changes: 26 additions & 0 deletions src/exm-utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* exm-utils.h
*
* Copyright 2022 Matthew Jakeman <mjakeman26@outlook.co.nz>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/

#pragma once

#include <gio/gio.h>

char *
exm_utils_read_resource (const char *resource, gsize *length);
4 changes: 2 additions & 2 deletions src/exm.gresource.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
<file>exm-comment-dialog.ui</file>
<file>exm-rating.ui</file>
<file>exm-release-notes-dialog.ui</file>
<file>res/plugin.png</file>
<file>release-notes.txt</file>
<file alias="suggestions.txt">res/suggestions.txt</file>
<file alias="release-notes.txt">res/release-notes.txt</file>
<file>style.css</file>
</gresource>
</gresources>
3 changes: 2 additions & 1 deletion src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ exm_sources = [

# Misc
'exm-release-notes-dialog.c',
'exm-install-button.c'
'exm-install-button.c',
'exm-utils.c'
]

exm_deps = [
Expand Down
File renamed without changes.
4 changes: 4 additions & 0 deletions src/res/suggestions.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Blur my Shell
Just Perfection
Night Theme Switcher
RunCat