summaryrefslogtreecommitdiffstats
path: root/src/schemes/about/profiles.c
blob: 3cb070962c562406bb742c3143ffa9029ea25b24 (plain)
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
/*
 * about:profiles
 *
 * Copyright (C) 2017  Patrick McDermott
 *
 * This file is part of Marquee.
 *
 * Marquee 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.
 *
 * Marquee 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 Marquee.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <string.h>

#include <glib.h>
#include <webkit2/webkit2.h>

#include "../../application.h"
#include "../../config/profiles.h"
#include "../../utils/html.h"
#include "../../utils/profile-icon.h"
#include "../about.h"
#include "paths.h"

static gchar *
generate_div(MqProfiles *profiles, gchar *id)
{
	gchar *name;
	gchar *color;
	gchar *editing_str;
	gchar *name_str;
	gchar *default_str;
	gchar *delete_str;
	gchar *save_str;
	gchar *div;

	/* name is freed by the mq_html_container() call to generate the name
	 * <span>. */
	if (id) {
		name = mq_profiles_get_name(profiles, id);
		color = mq_profiles_get_color(profiles, id);
	} else {
		name = g_strdup("New Profile");
		color = g_strdup("#00ff00");
	}

	editing_str = g_strconcat("editing_", id, NULL);
	name_str    = g_strconcat("name_",    id, NULL);
	default_str = g_strconcat("default_", id, NULL);
	delete_str  = g_strconcat("delete_",  id, NULL);
	save_str    = g_strconcat("save_",    id, NULL);

	div = mq_html_container("div", "profile",
		mq_html_input_radio("editing", editing_str, id, NULL,
			id == NULL),
		mq_profile_icon_new(color),
		mq_html_container("span", NULL, name, NULL),
		mq_html_input_text(name_str, NULL, name),
		mq_html_submit(default_str, "Make Default",
			!id || mq_profiles_is_default(profiles, id)),
		mq_html_submit(delete_str, "Delete", FALSE),
		mq_html_label(editing_str, "Edit", FALSE),
		mq_html_submit(save_str, "Save", FALSE),
		NULL);

	g_free(editing_str);
	g_free(name_str);
	g_free(default_str);
	g_free(delete_str);
	g_free(save_str);

	g_free(color);

	return div;
}

static gchar *
generate_document(MqProfiles *profiles, gboolean adding)
{
	gchar **ids;
	gsize   length;
	gchar **divs;
	gsize   i;

	ids = mq_profiles_get_profiles(profiles, &length);
	divs = g_new(gchar *, length + (adding ? 1 : 0) + 1);
	for (i = 0; i < length; ++i) {
		divs[i] = generate_div(profiles, ids[i]);
	}
	if (adding) {
		divs[i++] = generate_div(profiles, NULL);
	}
	divs[i] = NULL;
	g_strfreev(ids);

	return mq_html_document("Profiles",
		mq_html_form_v("add", "Add Profile", NULL, NULL, divs),
		NULL);
}

static void
edit_profiles(MqProfiles *profiles, GHashTable *query)
{
	GList *keys;
	GList *key;
	gchar *id;
	gchar *input_key;

	keys = g_hash_table_get_keys(query);

	for (key = keys; key; key = key->next) {
		if (g_str_has_prefix(key->data, "save_")) {
			id = (gchar *) key->data + strlen("save_");
			if (id[0]) {  /* Edit existing profile. */
				input_key = g_strconcat("name_", id, NULL);
				mq_profiles_set_name(profiles, id,
					g_hash_table_lookup(query, input_key));
				g_free(input_key);
			}
		} else if (g_str_has_prefix(key->data, "delete_")) {
			id = (gchar *) key->data + strlen("delete_");
			if (id[0]) {  /* Don't remove new profile. */
				mq_profiles_remove(profiles, id);
			}
		} else if (g_str_has_prefix(key->data, "default_")) {
			id = (gchar *) key->data + strlen("default_");
			if (id[0]) {  /* Don't set new profile as default. */
				mq_profiles_set_default(profiles, id);
			}
		}
	}

	g_list_free(keys);

	mq_profiles_save(profiles);
}

void
mq_about_profiles_response(MqApplication *application,
	GHashTable *query, WebKitURISchemeRequest *request)
{
	MqProfiles  *profiles;

	profiles = mq_application_get_profiles(application);

	if (!query) {
		mq_about_response(request, generate_document(profiles, FALSE));
	} else if (g_hash_table_lookup(query, "add")) {
		mq_about_response(request, generate_document(profiles, TRUE));
	} else {
		edit_profiles(profiles, query);
		mq_about_redirect(request, "mq-about:profiles");
	}
}