Back to index

lightning-sunbird  0.9+nobinonly
nsWelcomeDlg.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 "nsWelcomeDlg.h"
00041 #include "nsXInstaller.h"
00042 #include <sys/stat.h>
00043 
00044 
00045 nsWelcomeDlg::nsWelcomeDlg() :
00046     mReadmeFile(NULL)
00047 {
00048 }
00049 
00050 nsWelcomeDlg::~nsWelcomeDlg()
00051 {
00052     if (mReadmeFile)
00053         free (mReadmeFile);
00054 }
00055 
00056 void
00057 nsWelcomeDlg::Next(GtkWidget *aWidget, gpointer aData)
00058 {
00059     DUMP("Next");
00060     if (aData != gCtx->wdlg) return;
00061 #ifdef MOZ_WIDGET_GTK
00062     if (gCtx->bMoving) 
00063     {
00064         gCtx->bMoving = FALSE;
00065         return;
00066     }
00067 #endif
00068 
00069     // hide this notebook page
00070     gCtx->wdlg->Hide();
00071 
00072     // show the next dlg
00073     gCtx->ldlg->Show();
00074 #ifdef MOZ_WIDGET_GTK
00075     gCtx->bMoving = TRUE;
00076 #endif
00077 }
00078 
00079 int
00080 nsWelcomeDlg::Parse(nsINIParser *aParser)
00081 {
00082     int err = OK;
00083     int bufsize = 0;
00084     char *showDlg = NULL;
00085     
00086     /* compulsory keys*/
00087     XI_ERR_BAIL(aParser->GetStringAlloc(DLG_WELCOME, README, &mReadmeFile,
00088                                         &bufsize));
00089     if (bufsize == 0 || !mReadmeFile)
00090         return E_INVALID_KEY;
00091 
00092     /* optional keys */
00093     bufsize = 5;
00094     XI_ERR_BAIL(aParser->GetStringAlloc(DLG_WELCOME, SHOW_DLG, &showDlg,
00095                                         &bufsize));
00096     if (bufsize != 0 && showDlg)
00097     {
00098         if (0 == strncmp(showDlg, "TRUE", 4))
00099             mShowDlg = nsXInstallerDlg::SHOW_DIALOG;
00100         else if (0 == strncmp(showDlg, "FALSE", 5))
00101             mShowDlg = nsXInstallerDlg::SKIP_DIALOG;
00102     }
00103 
00104     bufsize = 0;
00105     XI_ERR_BAIL(aParser->GetStringAlloc(DLG_WELCOME, TITLE, &mTitle,
00106                                         &bufsize));
00107     if (bufsize == 0)
00108             XI_IF_FREE(mTitle); 
00109 
00110     return err;
00111 
00112 BAIL:
00113     return err;
00114 }
00115 
00116 int
00117 nsWelcomeDlg::Show()
00118 {
00119     int err = OK;
00120     if (!mShowDlg)
00121     {
00122        gCtx->ldlg->Show();
00123        return err;
00124     }
00125    
00126     char *readmeContents = NULL;
00127 
00128     XI_VERIFY(gCtx);
00129     XI_VERIFY(gCtx->notebook);
00130 
00131     if (mWidgetsInit == FALSE) // static widget init
00132     {
00133         // create a new table and add it as a page of the notebook
00134         mTable = gtk_table_new(1, 3, FALSE);
00135         gtk_notebook_append_page(GTK_NOTEBOOK(gCtx->notebook), mTable, NULL);
00136         mPageNum = gtk_notebook_get_current_page(GTK_NOTEBOOK(gCtx->notebook));
00137         // gtk_table_set_row_spacing(GTK_TABLE(mTable), 0, 0);
00138         gtk_table_set_col_spacing(GTK_TABLE(mTable), 1, 0);
00139         gtk_widget_show(mTable);
00140 
00141         // read the readme file contents into memory
00142         readmeContents = GetReadmeContents();
00143         if (!readmeContents)
00144         {
00145             err = ErrorHandler(E_EMPTY_README);
00146             goto BAIL;
00147         }
00148 
00149         // create a new scrollable textarea and add it to the table
00150 #if defined(MOZ_WIDGET_GTK)
00151         GtkWidget *text = gtk_text_new(NULL, NULL);
00152         GdkFont *font = gdk_font_load( README_FONT );
00153         gtk_text_set_editable(GTK_TEXT(text), FALSE);
00154         gtk_table_attach(GTK_TABLE(mTable), text, 1, 2, 0, 1,
00155                        static_cast<GtkAttachOptions>(GTK_FILL | GTK_EXPAND),
00156                        static_cast<GtkAttachOptions>(GTK_FILL | GTK_EXPAND),
00157                        0, 0);
00158         gtk_text_freeze(GTK_TEXT(text));
00159         gtk_text_insert (GTK_TEXT(text), font, &text->style->black, NULL,
00160                        readmeContents, -1);
00161         gtk_text_thaw(GTK_TEXT(text));
00162         gtk_text_set_word_wrap(GTK_TEXT(text), TRUE);
00163         gtk_widget_show(text);
00164 
00165         // Add a vertical scrollbar to the GtkText widget 
00166         GtkWidget *vscrollbar = gtk_vscrollbar_new (GTK_TEXT (text)->vadj);
00167         gtk_table_attach(GTK_TABLE(mTable), vscrollbar, 2, 3, 0, 1, GTK_FILL,
00168                        static_cast<GtkAttachOptions>(GTK_EXPAND | GTK_SHRINK | GTK_FILL),
00169                        0, 0);
00170         gtk_widget_show(vscrollbar);
00171 #elif defined(MOZ_WIDGET_GTK2)
00172         GtkWidget *text = gtk_scrolled_window_new (NULL, NULL);
00173         GtkWidget *textview = gtk_text_view_new();
00174         GtkTextBuffer *textbuffer;
00175 
00176         textbuffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview));
00177         gtk_text_buffer_set_text (textbuffer, readmeContents, -1);
00178         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (text),
00179                        GTK_POLICY_AUTOMATIC,
00180                        GTK_POLICY_AUTOMATIC);
00181         gtk_container_add (GTK_CONTAINER (text), textview);
00182         gtk_text_view_set_editable(GTK_TEXT_VIEW(textview), FALSE);
00183         gtk_text_view_set_cursor_visible (GTK_TEXT_VIEW(textview), FALSE);
00184         gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(textview), GTK_WRAP_WORD);
00185         gtk_table_attach_defaults(GTK_TABLE(mTable), text, 1, 2, 0, 1);
00186         gtk_widget_show_all(text);
00187 #endif
00188 
00189         mWidgetsInit = TRUE;
00190     }
00191     else
00192     {
00193         gtk_notebook_set_page(GTK_NOTEBOOK(gCtx->notebook), mPageNum);
00194         gtk_widget_show(mTable);
00195     }
00196 
00197     // signal connect the buttons
00198     gCtx->nextID = gtk_signal_connect(GTK_OBJECT(gCtx->next), "clicked",
00199                    GTK_SIGNAL_FUNC(nsWelcomeDlg::Next), gCtx->wdlg);
00200 
00201     GTK_WIDGET_SET_FLAGS(gCtx->next, GTK_CAN_DEFAULT);
00202     gtk_widget_grab_default(gCtx->next);
00203     gtk_widget_grab_focus(gCtx->next);
00204 
00205     // show the Next button
00206     gCtx->nextLabel = gtk_label_new(gCtx->Res("NEXT"));
00207     gtk_container_add(GTK_CONTAINER(gCtx->next), gCtx->nextLabel);
00208     gtk_widget_show(gCtx->nextLabel);
00209     gtk_widget_show(gCtx->next);
00210 
00211 BAIL:
00212     XI_IF_FREE(readmeContents);
00213 
00214     return err;
00215 }
00216 
00217 int 
00218 nsWelcomeDlg::Hide()
00219 {
00220     // hide all this dlg's widgets
00221     gtk_widget_hide(mTable);
00222 
00223     // disconnect and remove this dlg's nav btn
00224     gtk_signal_disconnect(GTK_OBJECT(gCtx->next), gCtx->nextID);
00225     gtk_container_remove(GTK_CONTAINER(gCtx->next), gCtx->nextLabel);
00226     gtk_widget_hide(gCtx->next);
00227 
00228     return OK;
00229 }
00230 
00231 int
00232 nsWelcomeDlg::SetReadmeFile(char *aReadmeFile)
00233 {
00234     if (!aReadmeFile)
00235         return E_PARAM;
00236 
00237     mReadmeFile = aReadmeFile;
00238 
00239     return OK;
00240 }
00241 
00242 char *
00243 nsWelcomeDlg::GetReadmeFile()
00244 {
00245     if (mReadmeFile)
00246         return mReadmeFile;
00247 
00248     return NULL;
00249 }
00250 
00251 char *
00252 nsWelcomeDlg::GetReadmeContents()
00253 {
00254     char *buf = NULL;
00255     FILE *fd = NULL;
00256     struct stat attr;
00257     int buflen;
00258 
00259     DUMP(mReadmeFile);
00260     if (!mReadmeFile)
00261         return NULL;
00262    
00263     // open file
00264     fd = fopen(mReadmeFile, "r");
00265     if (!fd) return NULL;
00266     DUMP("readme fopen");
00267 
00268     // get file length
00269     if (0 != stat(mReadmeFile, &attr)) return NULL;
00270     if (attr.st_size == 0) return NULL;
00271     DUMP("readme fstat");
00272 
00273     // allocate buffer of file length
00274     buflen = sizeof(char) * (attr.st_size + 1);
00275     buf = (char *) malloc(buflen);
00276     if (!buf) return NULL;
00277     memset(buf, 0, buflen);
00278     DUMP("readme buf malloc");
00279 
00280     // read entire file into buffer
00281     if (attr.st_size != ((int) fread(buf, sizeof(char), attr.st_size, fd))) 
00282         XI_IF_FREE(buf);
00283     DUMP("readme fread");
00284 
00285     // close file
00286     fclose(fd);
00287     DUMP("readme close");
00288 
00289     return buf;
00290 }