34573622419911ea97d43579d0ba335fa3fa3361
[modest] / src / modest-viewer-window.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30
31 /* modest-viewer-window.c */
32
33 #include "modest-ui.h"
34 #include "modest-tny-msg-view.h"
35 #include "modest-viewer-window.h"
36
37
38 /* 'private'/'protected' functions */
39 static void                      modest_viewer_window_class_init    (ModestViewerWindowClass *klass);
40 static void                      modest_viewer_window_init          (ModestViewerWindow *obj);
41 static void                      modest_viewer_window_finalize      (GObject *obj);
42
43 /* list my signals */
44 enum {
45         LAST_SIGNAL
46 };
47
48 typedef struct _ModestViewerWindowPrivate ModestViewerWindowPrivate;
49 struct _ModestViewerWindowPrivate {
50         ModestTnyMsgView *msg_view;
51         gpointer user_data;
52 };
53 #define MODEST_VIEWER_WINDOW_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
54                                                   MODEST_TYPE_VIEWER_WINDOW, \
55                                                   ModestViewerWindowPrivate))
56 /* globals */
57 static GtkWindowClass *parent_class = NULL;
58
59
60 GType
61 modest_viewer_window_get_type (void)
62 {
63         static GType my_type = 0;
64         if (!my_type) {
65                 static const GTypeInfo my_info = {
66                         sizeof(ModestViewerWindowClass),
67                         NULL,           /* base init */
68                         NULL,           /* base finalize */
69                         (GClassInitFunc) modest_viewer_window_class_init,
70                         NULL,           /* class finalize */
71                         NULL,           /* class data */
72                         sizeof(ModestViewerWindow),
73                         1,              /* n_preallocs */
74                         (GInstanceInitFunc) modest_viewer_window_init,
75                 };
76                 my_type = g_type_register_static (GTK_TYPE_WINDOW,
77                                                   "ModestViewerWindow",
78                                                   &my_info, 0);
79         }
80         return my_type;
81 }
82
83 static void
84 modest_viewer_window_class_init (ModestViewerWindowClass *klass)
85 {
86         GObjectClass *gobject_class;
87         gobject_class = (GObjectClass*) klass;
88
89         parent_class            = g_type_class_peek_parent (klass);
90         gobject_class->finalize = modest_viewer_window_finalize;
91
92         g_type_class_add_private (gobject_class, sizeof(ModestViewerWindowPrivate));
93
94         /* signal definitions go here, e.g.: */
95 /*      signals[MY_SIGNAL_1] = */
96 /*              g_signal_new ("my_signal_1",....); */
97 /*      signals[MY_SIGNAL_2] = */
98 /*              g_signal_new ("my_signal_2",....); */
99 /*      etc. */
100 }
101
102
103 static void
104 modest_viewer_window_init (ModestViewerWindow *obj)
105 {
106         ModestViewerWindowPrivate *priv = MODEST_VIEWER_WINDOW_GET_PRIVATE(obj);
107
108         priv->user_data = NULL;
109         priv->msg_view = NULL;
110 }
111
112
113 static void
114 modest_viewer_window_finalize (GObject *obj)
115 {
116         ModestViewerWindowPrivate *priv;
117
118         priv = MODEST_VIEWER_WINDOW_GET_PRIVATE(obj);
119                 
120         if (priv->user_data)
121                 g_free(priv->user_data);
122
123         G_OBJECT_CLASS(parent_class)->finalize (obj);
124 }
125
126
127 GtkWidget*
128 modest_viewer_window_new (ModestUI *ui, TnyMsgIface *msg)
129 {
130         GObject *self;
131         ModestViewerWindowPrivate *priv;
132         GtkWidget *w;
133         gpointer data;
134         GtkWidget *msg_view;
135
136         self = G_OBJECT(g_object_new(MODEST_TYPE_VIEWER_WINDOW, NULL));
137         priv = MODEST_VIEWER_WINDOW_GET_PRIVATE(self);
138
139         msg_view = modest_tny_msg_view_new(msg, FALSE);
140
141         data = NULL;
142         w = GTK_WIDGET(modest_ui_new_viewer_window(ui, msg_view, msg, &data));
143         if (!w)
144                 return NULL;
145         if (!data)
146                 g_message("viewer window user data is emtpy");
147
148         gtk_container_add(GTK_CONTAINER(self), w);
149         priv->user_data = data;
150         priv->msg_view = MODEST_TNY_MSG_VIEW(msg_view);
151
152         return GTK_WIDGET(self);
153 }
154
155
156 /*
157  * return user defined data from a ModestViewerWindow instance
158  * like e.g. a refernce to a GladeXML*
159  */
160 gpointer modest_viewer_window_get_data(ModestViewerWindow *viewer_win)
161 {
162         ModestViewerWindowPrivate *priv;
163
164         if (!viewer_win) {
165                 return NULL;
166         }
167         priv = MODEST_VIEWER_WINDOW_GET_PRIVATE(viewer_win);
168
169         return priv->user_data;
170 }
171
172
173 ModestTnyMsgView
174 *modest_viewer_window_get_tiny_msg_view(ModestViewerWindow *viewer_win)
175 {
176         ModestViewerWindowPrivate *priv;
177
178         if (!viewer_win) {
179                 return NULL;
180         }
181         priv = MODEST_VIEWER_WINDOW_GET_PRIVATE(viewer_win);
182
183         return priv->msg_view;
184 }