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
164
165
|
#include <stdlib.h>
#include <string.h>
#include <gtk/gtk.h>
#include <webkit/webkit.h>
#include "dwb.h"
#include "util.h"
#include "view.h"
/* dwb_session_get_groups() return char ** (alloc){{{*/
static char **
dwb_session_get_groups() {
char **groups = NULL;
char *content = dwb_util_get_file_content(dwb.files.session);
if (content) {
groups = g_regex_split_simple("^g:", content, G_REGEX_MULTILINE, G_REGEX_MATCH_NOTEMPTY);
free(content);
}
return groups;
}/*}}}*/
/* dwb_session_get_group(const char *) return char* (alloc){{{*/
static char *
dwb_session_get_group(const char *name) {
char *content = NULL;
char **groups = dwb_session_get_groups();
if (groups) {
int i=1;
char *group = g_strconcat(name, "\n", NULL);
while (groups[i]) {
if (g_str_has_prefix(groups[i], group)) {
content = g_strdup(groups[i]);
}
i++;
}
g_strfreev(groups);
FREE(group);
}
return content;
}/*}}}*/
/* dwb_session_load_webview(WebKitWebView *, char *, int *){{{*/
static void
dwb_session_load_webview(WebKitWebView *web, char *uri, int last) {
if (last > 0) {
for (int j=0; j<last; j++) {
webkit_web_view_go_back(web);
}
}
else {
webkit_web_view_load_uri(web, uri);
}
}/*}}}*/
/* dwb_session_list {{{*/
void
dwb_session_list() {
char *path = dwb_util_build_path();
dwb.files.session = g_build_filename(path, "session", NULL);
char **content = dwb_session_get_groups();
int i=1;
while (content[i]) {
char **group = g_strsplit(content[i], "\n", -1);
fprintf(stdout, "%d: %s\n", i++, group[0]);
g_strfreev(group);
}
g_strfreev(content);
FREE(path);
exit(EXIT_SUCCESS);
}/*}}}*/
/* dwb_session_restore(const char *name) {{{*/
gboolean
dwb_session_restore(const char *name) {
char *group = dwb_session_get_group(name);
if (!group) {
return false;
}
char **lines = g_strsplit(group, "\n", -1);
WebKitWebView *web, *lastweb = NULL;
WebKitWebBackForwardList *bf_list;
int last = 1;
char *uri = NULL;
int length = g_strv_length(lines) - 1;
for (int i=1; i<=length; i++) {
char **line = g_strsplit(lines[i], " ", 4);
if (line[0] && line[1] && line[2]) {
int current = strtol(line[0], NULL, 10);
if (current <= last) {
dwb_add_view(NULL);
web = CURRENT_WEBVIEW();
bf_list = webkit_web_back_forward_list_new_with_web_view(web);
if (lastweb) {
dwb_session_load_webview(lastweb, uri, last);
}
lastweb = web;
}
WebKitWebHistoryItem *item = webkit_web_history_item_new_with_data(line[1], line[2]);
webkit_web_back_forward_list_add_item(bf_list, item);
last = current;
FREE(uri);
uri = g_strdup(line[1]);
}
if (i == length && lastweb)
dwb_session_load_webview(lastweb, uri, last);
g_strfreev(line);
}
g_strfreev(lines);
gtk_widget_show_all(dwb.gui.window);
if (!dwb.state.views)
dwb_add_view(NULL);
if (dwb.state.layout & Maximized && dwb.state.views) {
gtk_widget_hide(dwb.gui.right);
for (GList *l = dwb.state.views->next; l; l=l->next) {
gtk_widget_hide(((View*)l->data)->vbox);
}
}
dwb_update_layout();
return true;
}/*}}}*/
/* dwb_session_save(const char *) {{{*/
gboolean
dwb_session_save(const char *name) {
if (!name) {
name = "default";
}
GString *buffer = g_string_new(NULL);
g_string_append_printf(buffer, "g:%s\n", name);
int view=0;
for (GList *l = g_list_last(dwb.state.views); l; l=l->prev, view++) {
WebKitWebView *web = WEBVIEW(l);
WebKitWebBackForwardList *bf_list = webkit_web_view_get_back_forward_list(web);
for (int i= -webkit_web_back_forward_list_get_back_length(bf_list); i<=webkit_web_back_forward_list_get_forward_length(bf_list); i++) {
WebKitWebHistoryItem *item = webkit_web_back_forward_list_get_nth_item(bf_list, i);
if (item) {
const char *uri = webkit_web_history_item_get_uri(item);
const char *title = webkit_web_history_item_get_title(item);
g_string_append_printf(buffer, "%d %s %s\n", i, uri, title);
}
}
}
char **groups = dwb_session_get_groups();
if (groups) {
int i=1;
char *group = g_strconcat(name, "\n", NULL);
while(groups[i]) {
if (!g_str_has_prefix(groups[i], group)) {
g_string_append_printf(buffer, "g:%s", groups[i]);
}
i++;
}
FREE(group);
g_strfreev(groups);
}
dwb_util_set_file_content(dwb.files.session, buffer->str);
g_string_free(buffer, true);
//dwb_exit();
return true;
}/*}}}*/
|