~directhex/libubuntuone/mono_packaging

« back to all changes in this revision

Viewing changes to libubuntuone/u1-music-store.c

  • Committer: Tarmac
  • Author(s): Rodrigo Moya
  • Date: 2010-01-26 23:23:55 UTC
  • mfrom: (19.1.1 separate-js-files)
  • Revision ID: rodrigo@megeve-20100126232355-qgyjy6w7yiqzmwuc
Move JS code out of the C source code

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
 
32
32
#define U1_DEFAULT_ERROR_PAGE "<html><body>Could not load Music Store</body></html>"
33
33
 
34
 
const gchar *U1_PREVIEW_JAVASCRIPT = "(function() {\n"
35
 
        "  // capture the preview\n"
36
 
        "  if (playTrack) {\n"
37
 
        "    playTrack = function(file, title) {\n"
38
 
        "      window.status = \"u1preview:::\" + file + \":::\" + title;\n"
39
 
        "    }\n"
40
 
        "  }\n"
41
 
        "  // and hide the flashplayer\n"
42
 
        "  var ply = document.getElementById(\"flashPlayer\");\n"
43
 
        "  if (ply) {\n"
44
 
        "    ply.parentNode.removeChild(ply);\n"
45
 
        "  }\n" 
46
 
        "})()";
47
 
const gchar *U1_LIBRARY_OVERRIDE_JAVASCRIPT = "(function(){\n"
48
 
        "  var lnks = document.querySelectorAll(\"a\");\n"
49
 
        "  for (var i=0; i<lnks.length; i++) {\n"
50
 
        "    if (lnks[i].href.indexOf(\"library.aspx\") != -1) {\n"
51
 
        "      lnks[i].href = \"%s\";\n"
52
 
        "    }\n"
53
 
        "  }\n"
54
 
        "})()";
55
 
 
56
34
struct _U1MusicStorePrivate {
57
35
        GtkWidget *web_viewer;
58
36
 
126
104
 
127
105
        return TRUE;
128
106
}
129
 
                         
 
107
 
 
108
static void
 
109
execute_script (WebKitWebView *web_view, const gchar *script_name, const gchar *replacement)
 
110
{
 
111
        gchar *file_contents, *script, *path;
 
112
        gsize length;
 
113
        GError *error = NULL;
 
114
 
 
115
        path = g_build_path ("/", U1_JAVASCRIPT_DIR, script_name, NULL);
 
116
 
 
117
        /* Load script from known location */
 
118
        if (g_file_get_contents (path, &file_contents, &length, &error)) {
 
119
                if (replacement != NULL)
 
120
                        script = g_strdup_printf (file_contents, replacement);
 
121
                else
 
122
                        script = g_strdup (file_contents);
 
123
 
 
124
                webkit_web_view_execute_script (web_view, script);
 
125
 
 
126
                g_free (script);
 
127
                g_free (file_contents);
 
128
        } else {
 
129
                g_warning ("Could not load %s: %s", path, error->message);
 
130
                g_error_free (error);
 
131
        }
 
132
 
 
133
        g_free (path);
 
134
}
 
135
 
130
136
static void
131
137
load_finished_cb (WebKitWebView *web_view, WebKitWebFrame *frame, gpointer user_data)
132
138
{
134
140
        U1MusicStore *music_store = U1_MUSIC_STORE (user_data);
135
141
 
136
142
        /* Inject some JavaScript to enable previews and hide the Flash player */
137
 
        webkit_web_view_execute_script (web_view, U1_PREVIEW_JAVASCRIPT);
 
143
        execute_script (web_view, "u1-preview.js", NULL);
138
144
 
139
145
        /* Allow us to specifically override the library page links */
140
146
        library_override = g_getenv ("U1MUSICLIBRARYURL");
141
 
        if (library_override != NULL) {
142
 
                gchar *script = g_strdup_printf (U1_LIBRARY_OVERRIDE_JAVASCRIPT, library_override);
143
 
 
144
 
                webkit_web_view_execute_script (web_view, script);
145
 
                g_free (script);
146
 
        }
 
147
        if (library_override != NULL)
 
148
                execute_script (web_view, "u1-library-override.js", library_override);
147
149
 
148
150
        gdk_window_set_cursor (GTK_WIDGET (web_view)->window, music_store->priv->normal_cursor);
149
151
}