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 the GNU General Public License Version 2 or later (the "GPL"), or
00028  * 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   mMsg0(NULL)
00047 {
00048 }
00049 
00050 nsLicenseDlg::~nsLicenseDlg()
00051 {
00052     XI_IF_FREE(mLicenseFile);
00053 }
00054 
00055 void
00056 nsLicenseDlg::Back(GtkWidget *aWidget, gpointer aData)
00057 {
00058     DUMP("Back");
00059     if (aData != gCtx->ldlg) return;
00060     
00061 // XXX call gCtx->me->Shutdown() ?
00062     gtk_main_quit();
00063     return;
00064 
00065 #if 0
00066     // hide this notebook page
00067     gCtx->ldlg->Hide(nsXInstallerDlg::BACKWARD_MOVE);
00068 
00069     // disconnect this dlg's nav btn signal handlers
00070     gtk_signal_disconnect(GTK_OBJECT(gCtx->back), gCtx->backID);
00071     gtk_signal_disconnect(GTK_OBJECT(gCtx->next), gCtx->nextID);
00072 
00073     // show the prev dlg
00074     gCtx->wdlg->Show(nsXInstallerDlg::BACKWARD_MOVE);
00075 #endif
00076 }
00077 
00078 void
00079 nsLicenseDlg::Next(GtkWidget *aWidget, gpointer aData)
00080 {
00081     DUMP("Next");
00082     if (aData != gCtx->ldlg) return;
00083 
00084     // hide this notebook page
00085     gCtx->ldlg->Hide(nsXInstallerDlg::FORWARD_MOVE);
00086 
00087     // disconnect this dlg's nav btn signal handlers
00088     gtk_signal_disconnect(GTK_OBJECT(gCtx->back), gCtx->backID);
00089     gtk_signal_disconnect(GTK_OBJECT(gCtx->next), gCtx->nextID);
00090 
00091     // show the next dlg
00092     gCtx->sdlg->Show(nsXInstallerDlg::FORWARD_MOVE);
00093 }
00094 
00095 int
00096 nsLicenseDlg::Parse(nsINIParser *aParser)
00097 {
00098     int err = OK;
00099     int bufsize = 0;
00100     char *showDlg = NULL;
00101     
00102     /* compulsory keys*/
00103     XI_ERR_BAIL(aParser->GetStringAlloc(DLG_LICENSE, LICENSE, 
00104                 &mLicenseFile, &bufsize));
00105     if (bufsize == 0 || !mLicenseFile)
00106         return E_INVALID_KEY;
00107 
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     aParser->GetStringAlloc(DLG_LICENSE, SUBTITLE, &mSubTitle, &bufsize);
00126     if (bufsize == 0)
00127       XI_IF_FREE(mSubTitle);
00128 
00129     aParser->GetStringAlloc(DLG_LICENSE, MSG0, &mMsg0, &bufsize);
00130     if (bufsize == 0)
00131       XI_IF_FREE(mMsg0);
00132 
00133 BAIL:
00134     return err;
00135 }
00136 
00137 int
00138 nsLicenseDlg::Show(int aDirection)
00139 {
00140     int err = OK;
00141     char *licenseContents = NULL;
00142     char *titleBuf;
00143 
00144     XI_VERIFY(gCtx);
00145     XI_VERIFY(gCtx->notebook);
00146 
00147     if (!mWidgetsInit) {
00148       // static widget init
00149 
00150       // Create a vbox with the message and scrolled window, and append it
00151       // as a page of the notebook.
00152 
00153       mBox = gtk_vbox_new(FALSE, 6);
00154       gtk_container_set_border_width(GTK_CONTAINER(mBox), 12);
00155 
00156       gtk_notebook_append_page(GTK_NOTEBOOK(gCtx->notebook), mBox, NULL);
00157       mPageNum = gtk_notebook_get_current_page(GTK_NOTEBOOK(gCtx->notebook));
00158 
00159       // Change "\n" in the label to a space and a literal newline character.
00160       char *newline = strstr(mMsg0, "\\n");
00161       if (newline) {
00162         newline[0] = ' ';
00163         newline[1] = '\n';
00164       }
00165 
00166       GtkWidget *msg0 = gtk_label_new(NULL);
00167       gtk_label_set_markup(GTK_LABEL(msg0), mMsg0);
00168       gtk_label_set_line_wrap(GTK_LABEL(msg0), TRUE);
00169       gtk_misc_set_alignment(GTK_MISC(msg0), 0.0, 0.5);
00170       gtk_box_pack_start(GTK_BOX(mBox), msg0, FALSE, FALSE, 0);
00171 
00172       GtkWidget *frame = gtk_frame_new(NULL);
00173       //      gtk_container_set_border_width(GTK_CONTAINER(frame), 10);
00174       gtk_box_pack_start(GTK_BOX(mBox), frame, TRUE, TRUE, 0);
00175 
00176       GtkWidget *scrollWindow = gtk_scrolled_window_new(NULL, NULL);
00177       gtk_container_add(GTK_CONTAINER(frame), scrollWindow);
00178 
00179       // read the license file contents into memory
00180       licenseContents = GetLicenseContents();
00181       if (!licenseContents) {
00182         err = ErrorHandler(E_EMPTY_LICENSE);
00183         goto BAIL;
00184       }
00185 
00186       GtkTextBuffer *buffer = gtk_text_buffer_new(NULL);
00187 
00188       GtkTextIter start_iter;
00189       gtk_text_buffer_get_start_iter(buffer, &start_iter);
00190       gtk_text_buffer_insert_with_tags(buffer, &start_iter, licenseContents,
00191                                        -1, /* monoTag, */ NULL);
00192 
00193       GtkWidget *text = gtk_text_view_new_with_buffer(buffer);
00194       gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD);
00195       gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
00196       gtk_text_view_set_left_margin(GTK_TEXT_VIEW(text), 2);
00197       gtk_text_view_set_right_margin(GTK_TEXT_VIEW(text), 2);
00198       //      gtk_text_view_set_top_margin(GTK_TEXT_VIEW(text), 2);
00199       //      gtk_text_view_set_bottom_margin(GTK_TEXT_VIEW(text), 2);
00200 
00201       gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollWindow),
00202                                      GTK_POLICY_AUTOMATIC,
00203                                      GTK_POLICY_AUTOMATIC);
00204 
00205       gtk_container_add(GTK_CONTAINER(scrollWindow), text);
00206 
00207       mWidgetsInit = TRUE;
00208     } else {
00209       gtk_notebook_set_page(GTK_NOTEBOOK(gCtx->notebook), mPageNum);
00210     }
00211 
00212     gtk_widget_show_all(mBox);
00213 
00214     // <b>title</b>\0
00215     titleBuf = new char[strlen(mTitle) + 9];
00216     sprintf(titleBuf, "<b>%s</b>", mTitle);
00217 
00218     gtk_label_set_markup(GTK_LABEL(gCtx->header_title), titleBuf);
00219     gtk_label_set_text(GTK_LABEL(gCtx->header_subtitle), mSubTitle);
00220 
00221     delete[] titleBuf;
00222 
00223     // Set up the header if we're moving forward.
00224     if (aDirection == nsXInstallerDlg::FORWARD_MOVE) {
00225       GtkStyle *style = gtk_widget_get_style(gCtx->header);
00226       if (style->bg_pixmap[GTK_STATE_NORMAL]) {
00227         gint width, height;
00228         gdk_drawable_get_size(style->bg_pixmap[GTK_STATE_NORMAL],
00229                               &width, &height);
00230         gtk_widget_set_size_request(gCtx->header, -1, height);
00231       }
00232     }
00233 
00234     // signal connect the buttons
00235     gCtx->backID = gtk_signal_connect(GTK_OBJECT(gCtx->back), "clicked",
00236                    GTK_SIGNAL_FUNC(nsLicenseDlg::Back), gCtx->ldlg);
00237     gCtx->nextID = gtk_signal_connect(GTK_OBJECT(gCtx->next), "clicked",
00238                    GTK_SIGNAL_FUNC(nsLicenseDlg::Next), gCtx->ldlg);
00239 
00240     // enable back button if we came from the welcome dlg
00241     if (aDirection == nsXInstallerDlg::FORWARD_MOVE)
00242         if (gCtx->back)
00243             gtk_widget_set_sensitive(gCtx->back, TRUE); 
00244 
00245     // always change the button titles to Accept/Decline
00246     gtk_button_set_label(GTK_BUTTON(gCtx->next), gCtx->Res("ACCEPT"));
00247     gtk_button_set_label(GTK_BUTTON(gCtx->back), gCtx->Res("DECLINE"));
00248 
00249 BAIL:
00250     XI_IF_FREE(licenseContents);
00251     return err;
00252 }
00253 
00254 int
00255 nsLicenseDlg::Hide(int aDirection)
00256 {
00257     // hide all this dlg's widgets
00258   gtk_widget_hide(mBox);
00259 
00260     return OK;
00261 }
00262 
00263 int
00264 nsLicenseDlg::SetLicenseFile(char *aLicenseFile)
00265 {
00266     if (!aLicenseFile)
00267         return E_PARAM;
00268 
00269     mLicenseFile = aLicenseFile;
00270 
00271     return OK;
00272 }
00273 
00274 char *
00275 nsLicenseDlg::GetLicenseFile()
00276 {
00277     if (mLicenseFile)
00278         return mLicenseFile;
00279 
00280     return NULL;
00281 }
00282 
00283 char *
00284 nsLicenseDlg::GetLicenseContents()
00285 {
00286     char *buf = NULL;
00287     FILE *fd = NULL;
00288     struct stat attr;
00289     int buflen;
00290 
00291     DUMP(mLicenseFile);
00292     if (!mLicenseFile)
00293         return NULL;
00294    
00295     // open file
00296     fd = fopen(mLicenseFile, "r");
00297     if (!fd) return NULL;
00298     DUMP("license fopen");
00299 
00300     // get file length
00301     if (0 != stat(mLicenseFile, &attr)) return NULL;
00302     if (attr.st_size == 0) return NULL;
00303     DUMP("license fstat");
00304 
00305     // allocate buffer of file length
00306     buflen = sizeof(char) * (attr.st_size + 1);
00307     buf = (char *) malloc(buflen);
00308     if (!buf) return NULL;
00309     memset(buf, 0, buflen);
00310     DUMP("license buf malloc");
00311 
00312     // read entire file into buffer
00313     if (attr.st_size != ((int) fread(buf, sizeof(char), attr.st_size, fd))) 
00314         XI_IF_FREE(buf);
00315     DUMP("license fread");
00316 
00317     // close file
00318     fclose(fd);
00319     DUMP("license close");
00320 
00321     return buf;
00322 }