Back to index

lightning-sunbird  0.9+nobinonly
nsLicenseDlg.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla Communicator client code, released
00016  * March 31, 1998.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 1998
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Samir Gehani <sgehani@netscape.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #include "nsLicenseDlg.h"
00041 #include "nsXInstaller.h"
00042 #include <sys/stat.h>
00043 
00044 nsLicenseDlg::nsLicenseDlg() :
00045     mLicenseFile(NULL)
00046 {
00047 }
00048 
00049 nsLicenseDlg::~nsLicenseDlg()
00050 {
00051     XI_IF_FREE(mLicenseFile);
00052 }
00053 
00054 void
00055 nsLicenseDlg::Back(GtkWidget *aWidget, gpointer aData)
00056 {
00057     DUMP("Back");
00058     if (aData != gCtx->ldlg) return;
00059 #ifdef MOZ_WIDGET_GTK
00060     if (gCtx->bMoving) 
00061     {
00062         gCtx->bMoving = FALSE;
00063         return;
00064     }
00065 #endif
00066     
00067     gtk_main_quit();
00068     return;
00069 }
00070 
00071 void
00072 nsLicenseDlg::Next(GtkWidget *aWidget, gpointer aData)
00073 {
00074     DUMP("Next");
00075     if (aData != gCtx->ldlg) return;
00076 #ifdef MOZ_WIDGET_GTK
00077     if (gCtx->bMoving) 
00078     {
00079         gCtx->bMoving = FALSE;
00080         return;
00081     }
00082 #endif
00083 
00084     // hide this notebook page
00085     gCtx->ldlg->Hide();
00086 
00087     // show the next dlg
00088     gCtx->sdlg->Show();
00089 #ifdef MOZ_WIDGET_GTK
00090     gCtx->bMoving = TRUE;
00091 #endif
00092 }
00093 
00094 int
00095 nsLicenseDlg::Parse(nsINIParser *aParser)
00096 {
00097     int err = OK;
00098     int bufsize = 0;
00099     char *showDlg = NULL;
00100     
00101     /* compulsory keys*/
00102     XI_ERR_BAIL(aParser->GetStringAlloc(DLG_LICENSE, LICENSE, 
00103                 &mLicenseFile, &bufsize));
00104     if (bufsize == 0 || !mLicenseFile)
00105         return E_INVALID_KEY;
00106 
00107     /* optional keys */
00108     bufsize = 5;
00109     XI_ERR_BAIL(aParser->GetStringAlloc(DLG_LICENSE, SHOW_DLG, &showDlg,
00110                                         &bufsize));
00111     if (bufsize != 0 && showDlg)
00112     {
00113         if (0 == strncmp(showDlg, "TRUE", 4))
00114             mShowDlg = nsXInstallerDlg::SHOW_DIALOG;
00115         else if (0 == strncmp(showDlg, "FALSE", 5))
00116             mShowDlg = nsXInstallerDlg::SKIP_DIALOG;
00117     }
00118 
00119     bufsize = 0;
00120     XI_ERR_BAIL(aParser->GetStringAlloc(DLG_LICENSE, TITLE, &mTitle,
00121                                         &bufsize));
00122     if (bufsize == 0)
00123             XI_IF_FREE(mTitle); 
00124 
00125     return err;
00126 
00127 BAIL:
00128     return err;
00129 }
00130 
00131 int
00132 nsLicenseDlg::Show()
00133 {
00134     int err = OK;
00135     if (!mShowDlg)
00136     {
00137        gCtx->sdlg->Show();
00138        return err;
00139     }
00140     
00141     char *licenseContents = NULL;
00142 
00143     XI_VERIFY(gCtx);
00144     XI_VERIFY(gCtx->notebook);
00145 
00146     if (mWidgetsInit == FALSE) // static widget init
00147     {
00148         // create a new table and add it as a page of the notebook
00149         mTable = gtk_table_new(1, 3, FALSE);
00150         gtk_notebook_append_page(GTK_NOTEBOOK(gCtx->notebook), mTable, NULL);
00151         mPageNum = gtk_notebook_get_current_page(GTK_NOTEBOOK(gCtx->notebook));
00152         // gtk_table_set_row_spacing(GTK_TABLE(mTable), 0, 0);
00153         gtk_table_set_col_spacing(GTK_TABLE(mTable), 1, 0);
00154         gtk_widget_show(mTable);
00155 
00156         // read the license file contents into memory
00157         licenseContents = GetLicenseContents();
00158         if (!licenseContents)
00159         {
00160             err = ErrorHandler(E_EMPTY_LICENSE);
00161             goto BAIL;
00162         }
00163 
00164 #if defined(MOZ_WIDGET_GTK)
00165         // create a new scrollable textarea and add it to the table
00166         GtkWidget *text = gtk_text_new(NULL, NULL);
00167         GdkFont *font = gdk_font_load( LICENSE_FONT );
00168         gtk_text_set_editable(GTK_TEXT(text), FALSE);
00169         gtk_text_set_word_wrap(GTK_TEXT(text), TRUE);
00170         gtk_text_set_line_wrap(GTK_TEXT(text), TRUE);
00171         gtk_table_attach(GTK_TABLE(mTable), text, 1, 2, 0, 1,
00172             static_cast<GtkAttachOptions>(GTK_FILL | GTK_EXPAND),
00173             static_cast<GtkAttachOptions>(GTK_FILL | GTK_EXPAND),
00174                      0, 0);
00175         gtk_text_freeze(GTK_TEXT(text));
00176         gtk_text_insert (GTK_TEXT(text), font, &text->style->black, NULL,
00177                           licenseContents, -1);
00178         gtk_text_thaw(GTK_TEXT(text));
00179         gtk_widget_show(text);
00180 
00181         // Add a vertical scrollbar to the GtkText widget 
00182         GtkWidget *vscrollbar = gtk_vscrollbar_new (GTK_TEXT (text)->vadj);
00183         gtk_table_attach(GTK_TABLE(mTable), vscrollbar, 2, 3, 0, 1,
00184             GTK_FILL,
00185                      static_cast<GtkAttachOptions>(GTK_EXPAND | GTK_SHRINK | GTK_FILL),
00186                      0, 0);
00187         gtk_widget_show(vscrollbar);
00188 #elif defined(MOZ_WIDGET_GTK2)
00189         GtkWidget *text = gtk_scrolled_window_new (NULL, NULL);
00190         GtkWidget *textview = gtk_text_view_new();
00191         GtkTextBuffer *textbuffer;
00192 
00193         textbuffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview));
00194         gtk_text_buffer_set_text (textbuffer, licenseContents, -1);
00195         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (text),
00196             GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
00197         gtk_container_add (GTK_CONTAINER (text), textview);
00198         gtk_text_view_set_editable(GTK_TEXT_VIEW(textview), FALSE);
00199         gtk_text_view_set_cursor_visible (GTK_TEXT_VIEW(textview), FALSE);
00200         gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(textview), GTK_WRAP_WORD);
00201         gtk_table_attach(GTK_TABLE(mTable), text, 1, 2, 0, 1,
00202             static_cast<GtkAttachOptions>(GTK_FILL | GTK_EXPAND ),
00203             static_cast<GtkAttachOptions>(GTK_FILL | GTK_EXPAND), 0, 0);
00204         gtk_widget_show_all(text);
00205 #endif
00206 
00207         mWidgetsInit = TRUE;
00208     }
00209     else
00210     {
00211         gtk_notebook_set_page(GTK_NOTEBOOK(gCtx->notebook), mPageNum);
00212         gtk_widget_show(mTable);
00213     }
00214 
00215     // signal connect the buttons
00216     gCtx->backID = gtk_signal_connect(GTK_OBJECT(gCtx->back), "clicked",
00217                    GTK_SIGNAL_FUNC(nsLicenseDlg::Back), gCtx->ldlg);
00218     gCtx->nextID = gtk_signal_connect(GTK_OBJECT(gCtx->next), "clicked",
00219                    GTK_SIGNAL_FUNC(nsLicenseDlg::Next), gCtx->ldlg);
00220 
00221     GTK_WIDGET_SET_FLAGS(gCtx->next, GTK_CAN_DEFAULT);
00222     gtk_widget_grab_default(gCtx->next);
00223     gtk_widget_grab_focus(gCtx->next);
00224     
00225     // always change the button titles to Accept/Decline
00226     gCtx->acceptLabel = gtk_label_new(gCtx->Res("ACCEPT"));
00227     gCtx->declineLabel = gtk_label_new(gCtx->Res("DECLINE"));
00228     gtk_container_add(GTK_CONTAINER(gCtx->next), gCtx->acceptLabel);
00229     gtk_container_add(GTK_CONTAINER(gCtx->back), gCtx->declineLabel);
00230     gtk_widget_show(gCtx->acceptLabel);
00231     gtk_widget_show(gCtx->declineLabel);
00232     gtk_widget_show(gCtx->next);
00233     gtk_widget_show(gCtx->back);
00234 
00235 BAIL:
00236     XI_IF_FREE(licenseContents);
00237     return err;
00238 }
00239 
00240 int
00241 nsLicenseDlg::Hide()
00242 {
00243     // hide all this dlg's widgets
00244     gtk_widget_hide(mTable);
00245 
00246     // disconnect and remove this dlg's nav btns
00247     gtk_signal_disconnect(GTK_OBJECT(gCtx->back), gCtx->backID);
00248     gtk_signal_disconnect(GTK_OBJECT(gCtx->next), gCtx->nextID);
00249 
00250     gtk_container_remove(GTK_CONTAINER(gCtx->back), gCtx->declineLabel);
00251     gtk_container_remove(GTK_CONTAINER(gCtx->next), gCtx->acceptLabel);
00252 
00253     gtk_widget_hide(gCtx->back);
00254     gtk_widget_hide(gCtx->next);
00255     
00256     return OK;
00257 }
00258 
00259 int
00260 nsLicenseDlg::SetLicenseFile(char *aLicenseFile)
00261 {
00262     if (!aLicenseFile)
00263         return E_PARAM;
00264 
00265     mLicenseFile = aLicenseFile;
00266 
00267     return OK;
00268 }
00269 
00270 char *
00271 nsLicenseDlg::GetLicenseFile()
00272 {
00273     if (mLicenseFile)
00274         return mLicenseFile;
00275 
00276     return NULL;
00277 }
00278 
00279 char *
00280 nsLicenseDlg::GetLicenseContents()
00281 {
00282     char *buf = NULL;
00283     FILE *fd = NULL;
00284     struct stat attr;
00285     int buflen;
00286 
00287     DUMP(mLicenseFile);
00288     if (!mLicenseFile)
00289         return NULL;
00290    
00291     // open file
00292     fd = fopen(mLicenseFile, "r");
00293     if (!fd) return NULL;
00294     DUMP("license fopen");
00295 
00296     // get file length
00297     if (0 != stat(mLicenseFile, &attr)) return NULL;
00298     if (attr.st_size == 0) return NULL;
00299     DUMP("license fstat");
00300 
00301     // allocate buffer of file length
00302     buflen = sizeof(char) * (attr.st_size + 1);
00303     buf = (char *) malloc(buflen);
00304     if (!buf) return NULL;
00305     memset(buf, 0, buflen);
00306     DUMP("license buf malloc");
00307 
00308     // read entire file into buffer
00309     if (attr.st_size != ((int) fread(buf, sizeof(char), attr.st_size, fd))) 
00310         XI_IF_FREE(buf);
00311     DUMP("license fread");
00312 
00313     // close file
00314     fclose(fd);
00315     DUMP("license close");
00316 
00317     return buf;
00318 }