Factor updater.ini parsing out into a separate source module for use under OSX.

Cleanup GTK progress UI somewhat.
This commit is contained in:
darin%meer.net 2005-06-14 23:09:52 +00:00
parent 00e3ec09ac
commit 780c7fb24b
4 changed files with 173 additions and 58 deletions

View File

@ -73,7 +73,10 @@ endif
ifneq ($(MOZ_ENABLE_GTK2),)
HAVE_PROGRESSUI = 1
CPPSRCS += progressui_gtk.cpp
CPPSRCS += \
progressui_gtk.cpp \
readstrings.cpp \
$(NULL)
OS_CXXFLAGS += $(TK_CFLAGS)
OS_LIBS += $(TK_LIBS)
endif

View File

@ -39,11 +39,11 @@
#include <stdio.h>
#include <gtk/gtk.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include "progressui.h"
#include "readstrings.h"
#include "errors.h"
#define TIMER_INTERVAL 100
#define MAX_TEXT_LEN 200
static float sProgressVal; // between 0 and 100
static gboolean sQuit = FALSE;
@ -55,53 +55,6 @@ static GtkWidget *sProgressBar;
static const char *sProgramPath;
// stack based FILE wrapper to ensure that fclose is called.
class AutoFILE {
public:
AutoFILE(FILE *fp) : fp_(fp) {}
~AutoFILE() { if (fp_) fclose(fp_); }
operator FILE *() { return fp_; }
private:
FILE *fp_;
};
// very basic parser for updater.ini
static gboolean
ReadStrings(char title[MAX_TEXT_LEN], char info[MAX_TEXT_LEN])
{
char path[PATH_MAX];
snprintf(path, sizeof(path), "%s.ini", sProgramPath);
AutoFILE fp = fopen(path, "r");
if (!fp)
return FALSE;
if (!fgets(title, MAX_TEXT_LEN, fp))
return FALSE;
if (!fgets(title, MAX_TEXT_LEN, fp))
return FALSE;
if (!fgets(info, MAX_TEXT_LEN, fp))
return FALSE;
// trim trailing newline character and leading 'key='
char *strings[] = {
title, info, NULL
};
for (char **p = strings; *p; ++p) {
int len = strlen(*p);
if (len)
(*p)[len - 1] = '\0';
char *eq = strchr(*p, '=');
if (!eq)
return FALSE;
memmove(*p, eq + 1, len - (eq - *p + 1));
}
return TRUE;
}
static gboolean
UpdateDialog(gpointer data)
{
@ -119,9 +72,10 @@ UpdateDialog(gpointer data)
return TRUE;
}
static void
DoNothing()
static gboolean
OnDeleteEvent(GtkWidget *widget, GdkEvent *event, gpointer user_data)
{
return TRUE;
}
int
@ -144,22 +98,35 @@ ShowProgressUI()
if (sQuit || sProgressVal > 50.0f)
return 0;
char titleText[MAX_TEXT_LEN], infoText[MAX_TEXT_LEN];
if (!ReadStrings(titleText, infoText))
char path[PATH_MAX];
snprintf(path, sizeof(path), "%s.ini", sProgramPath);
StringTable strings;
if (ReadStrings(path, &strings) != OK)
return -1;
sWin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
if (!sWin)
return -1;
gtk_window_set_title(GTK_WINDOW(sWin), titleText);
// GTK 2.2 seems unable to prevent our dialog from being closed when
// the user hits the close button on the dialog. This problem only
// occurs when either one of the following methods are called:
// gtk_window_set_position
// gtk_window_set_type_hint
// For this reason, we disable window decorations.
g_signal_connect(G_OBJECT(sWin), "delete_event",
G_CALLBACK(OnDeleteEvent), NULL);
gtk_window_set_title(GTK_WINDOW(sWin), strings.title);
gtk_window_set_type_hint(GTK_WINDOW(sWin), GDK_WINDOW_TYPE_HINT_DIALOG);
gtk_window_set_position(GTK_WINDOW(sWin), GTK_WIN_POS_CENTER_ALWAYS);
gtk_window_set_resizable(GTK_WINDOW(sWin), FALSE);
gtk_window_set_decorated(GTK_WINDOW(sWin), FALSE);
gtk_signal_connect(GTK_OBJECT(sWin), "delete", DoNothing, NULL);
GtkWidget *vbox = gtk_vbox_new(TRUE, 6);
sLabel = gtk_label_new(infoText);
sLabel = gtk_label_new(strings.info);
gtk_misc_set_alignment(GTK_MISC(sLabel), 0.0f, 0.0f);
sProgressBar = gtk_progress_bar_new();

View File

@ -0,0 +1,91 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is Google Inc.
* Portions created by the Initial Developer are Copyright (C) 2005
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Darin Fisher <darin@meer.net>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include <limits.h>
#include <string.h>
#include <stdio.h>
#include "readstrings.h"
#include "errors.h"
// stack based FILE wrapper to ensure that fclose is called.
class AutoFILE {
public:
AutoFILE(FILE *fp) : fp_(fp) {}
~AutoFILE() { if (fp_) fclose(fp_); }
operator FILE *() { return fp_; }
private:
FILE *fp_;
};
// very basic parser for updater.ini
int
ReadStrings(const char *path, StringTable *results)
{
AutoFILE fp = fopen(path, "r");
if (!fp)
return IO_ERROR;
// Trim leading junk -- this is a hack!
if (!fgets(results->title, MAX_TEXT_LEN, fp))
return IO_ERROR;
if (!fgets(results->title, MAX_TEXT_LEN, fp))
return IO_ERROR;
// Now, read the values we care about.
if (!fgets(results->title, MAX_TEXT_LEN, fp))
return IO_ERROR;
if (!fgets(results->info, MAX_TEXT_LEN, fp))
return IO_ERROR;
// Trim trailing newline character and leading 'key='
char *strings[] = {
results->title, results->info, NULL
};
for (char **p = strings; *p; ++p) {
int len = strlen(*p);
if (len)
(*p)[len - 1] = '\0';
char *eq = strchr(*p, '=');
if (!eq)
return PARSE_ERROR;
memmove(*p, eq + 1, len - (eq - *p + 1));
}
return OK;
}

View File

@ -0,0 +1,54 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is Google Inc.
* Portions created by the Initial Developer are Copyright (C) 2005
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Darin Fisher <darin@meer.net>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#ifndef READSTRINGS_H__
#define READSTRINGS_H__
#define MAX_TEXT_LEN 200
struct StringTable {
char title[MAX_TEXT_LEN];
char info[MAX_TEXT_LEN];
};
/**
* This function reads in localized strings from updater.ini
*/
int ReadStrings(const char *path, StringTable *results);
#endif // READSTRINGS_H__