2009-09-12 Chris Toshok <toshok@ximian.com>
[moon.git] / src / pipeline-nocodec-ui.cpp
blobf7d85f77fa0f6a40cf2f211a8393ec506db3be3f
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * pipeline-nocodec-ui.cpp:
5 * Contact:
6 * Moonlight List (moonlight-list@lists.ximian.com)
8 * Copyright 2007 Novell, Inc. (http://www.novell.com)
10 * See the LICENSE file included with the distribution for details.
13 #include <config.h>
14 #include <errno.h>
15 #include <string.h>
16 #include <fcntl.h>
17 #include <stdlib.h>
18 #include <gdk-pixbuf/gdk-pixbuf.h>
20 #include "pipeline-nocodec-ui.h"
21 #include "downloader.h"
22 #include "utils.h"
23 #include "pipeline.h"
24 #include "debug.h"
26 bool CodecDownloader::running = false;
28 CodecDownloader::CodecDownloader (Surface *surf)
30 surface = surf;
31 state = 0;
32 dialog = NULL;
33 vbox = NULL;
34 header_label = NULL;
35 message_label = NULL;
36 ok_button = NULL;
37 icon = NULL;
38 dont_ask = NULL;
41 CodecDownloader::~CodecDownloader ()
43 running = false;
46 void
47 CodecDownloader::ShowUI (Surface *surface)
49 if (running) {
50 return;
53 if (!(moonlight_flags & RUNTIME_INIT_ENABLE_MS_CODECS))
54 return;
56 CodecDownloader *cd = new CodecDownloader (surface);
57 cd->Show ();
58 cd->unref ();
61 // ----- Event Proxies -----
63 void
64 CodecDownloader::ResponseEventHandler (GtkDialog *dialog, gint response, gpointer data)
66 ((CodecDownloader *) data)->ResponseEvent (dialog, (GtkResponseType)response);
69 // ----- Event Handlers -----
71 void
72 CodecDownloader::ResponseEvent (GtkDialog *dialog, GtkResponseType response)
74 LOG_UI ("CodecDownloader::ResponseEvent (%d)\n", response);
75 SetCurrentDeployment ();
77 switch (response) {
78 case GTK_RESPONSE_DELETE_EVENT:
79 Close ();
80 return;
81 case GTK_RESPONSE_OK:
82 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dont_ask))) {
83 LOG_UI ("Setting DontWarnUnsupportedCodecs\n");
84 configuration.SetBooleanValue ("Codecs", "DontWarnUnsupportedCodecs", true);
85 configuration.Save ();
87 Close ();
88 return;
89 default:
90 return;
94 void
95 CodecDownloader::SetHeader (const gchar *message)
97 gchar *message_full = g_strdup_printf ("<big><b>%s</b></big>", message);
98 gtk_label_set_markup (GTK_LABEL (header_label), message_full);
99 g_free (message_full);
102 void
103 CodecDownloader::SetMessage (const gchar *message)
105 gtk_label_set_text (GTK_LABEL (message_label), message);
106 gtk_widget_show (message_label);
109 void
110 CodecDownloader::HideMessage ()
112 gtk_widget_hide (message_label);
115 void
116 CodecDownloader::AdaptToParentWindow ()
118 // try to find a parent for our window
119 // there must be a better way of doing this though :|
120 GList *toplevels = gtk_window_list_toplevels ();
121 GList *current = toplevels;
122 GtkWindow *parent = NULL;
124 while (current != NULL) {
125 const char *title = gtk_window_get_title (GTK_WINDOW (current->data));
126 if (title != NULL && strstr (title, "Mozilla Firefox") != NULL) {
127 parent = GTK_WINDOW (current->data);
128 break;
131 current = current->next;
133 g_list_free (toplevels);
135 if (parent != NULL) {
136 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
137 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER_ON_PARENT);
138 } else {
139 // If no parent could be found, just center in the screen
140 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER);
144 // ----- Dialog Create/Destroy -----
146 void
147 CodecDownloader::Show ()
149 if (configuration.GetBooleanValue ("Codecs", "DontWarnUnsupportedCodecs")) {
150 state = 1;
151 return;
154 if (state != 0) {
155 fprintf (stderr, "CodecDownloader::Show (): Can't call Show more than once.\n");
156 state = 2;
157 return;
160 gint label_width = 400;
162 // Build HIG Dialog Box
163 dialog = gtk_dialog_new_with_buttons ("Moonlight Codecs Installer", NULL, (GtkDialogFlags)
164 (GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_NO_SEPARATOR), NULL);
165 ok_button = gtk_dialog_add_button (GTK_DIALOG (dialog), "_Ok", GTK_RESPONSE_OK);
166 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
168 AdaptToParentWindow ();
169 gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
170 gtk_object_set (GTK_OBJECT (dialog), "resizable", false, NULL);
172 // HIG HBox
173 GtkWidget *hbox = gtk_hbox_new (false, 12);
174 gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
175 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), hbox, true, true, 0);
177 // Message box icon
178 icon = gtk_image_new_from_stock (GTK_STOCK_DIALOG_WARNING, GTK_ICON_SIZE_DIALOG);
179 gtk_misc_set_alignment (GTK_MISC (icon), 0.5f, 0.0f);
180 gtk_box_pack_start (GTK_BOX (hbox), icon, false, false, 0);
182 // Contents container
183 vbox = gtk_vbox_new (false, 0);
184 gtk_box_set_spacing (GTK_BOX (vbox), 10);
185 gtk_box_pack_start (GTK_BOX (hbox), vbox, true, true, 0);
187 // Header Label
188 header_label = gtk_label_new (NULL);
189 SetHeader ("This page requires the Microsoft Media Pack to be installed to play multimedia content.");
190 gtk_label_set_line_wrap (GTK_LABEL (header_label), true);
191 gtk_label_set_justify (GTK_LABEL (header_label), GTK_JUSTIFY_LEFT);
192 gtk_misc_set_alignment (GTK_MISC (header_label), 0.0f, 0.5f);
193 gtk_widget_set_size_request (header_label, label_width, -1);
194 gtk_box_pack_start (GTK_BOX (vbox), header_label, false, false, 0);
196 // Secondary Label
197 message_label = gtk_label_new (NULL);
198 SetMessage ("The Microsoft Media Pack is currently unavailable for your "
199 "Operating System or Architecture.");
200 gtk_label_set_line_wrap (GTK_LABEL (message_label), true);
201 gtk_label_set_justify (GTK_LABEL (message_label), GTK_JUSTIFY_LEFT);
202 gtk_misc_set_alignment (GTK_MISC (message_label), 0.0f, 0.5f);
203 gtk_widget_set_size_request (message_label, label_width, -1);
204 gtk_box_pack_start (GTK_BOX (vbox), message_label, false, false, 0);
206 dont_ask = gtk_check_button_new_with_label ("Do not show me this message again");
207 gtk_box_pack_start (GTK_BOX (vbox), dont_ask, false, false, 0);
209 // Connect and go
210 g_signal_connect (G_OBJECT (dialog), "response", G_CALLBACK (ResponseEventHandler), this);
212 gtk_object_set (GTK_OBJECT (ok_button), "has-focus", true, "has-default", true, NULL);
214 gtk_widget_show_all (dialog);
216 ref (); // We manage our lifetime ourself
217 running = true;
220 void
221 CodecDownloader::Close ()
223 LOG_UI ("CodecDownloader::Close ()\n");
225 gtk_widget_destroy (dialog);
226 unref ();
227 running = false;