[Pkg-wmaker-commits] [wmforecast] 03/63: Improved error handling, display bug report email in --help for help2man, and better freeing of Weather struct

Doug Torrance dtorrance-guest at moszumanska.debian.org
Mon Aug 17 11:20:32 UTC 2015


This is an automated email from the git hooks/post-receive script.

dtorrance-guest pushed a commit to branch master
in repository wmforecast.

commit 577d59bd3117fba70b3d7762829ee6f8a963f56a
Author: Doug Torrance <dtorrance at monmouthcollege.edu>
Date:   Fri Apr 18 08:09:24 2014 -0500

    Improved error handling, display bug report email in --help for help2man, and better freeing of Weather struct
---
 ChangeLog    |  13 +++++--
 configure    |  11 ++++--
 wmforecast.1 |   2 +-
 wmforecast.c | 122 ++++++++++++++++++++++++++++++++++++-----------------------
 4 files changed, 93 insertions(+), 55 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 0a4c3ea..965893c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,11 +1,18 @@
+2015-04-17  Doug Torrance <dtorrance at monmouthcollege.edu>
+	* wmforecast.c: improved error handling -- Weather struct now contains
+	  errorFlag and errorText.  If errorFlag set to 1, "ERROR" is displayed
+	  in dockapp along along with an icon with a question mark, and
+	  errorText is displayed in a balloon tooltip, instead of drawing the
+	  dockapp as usual.  Previous calls to libcurl and libxml that used
+	  stderr for errors now use this.  This avoids, e.g., a segfault when
+	  the internet is down.
+
 2015-04-16  Doug Torrance <dtorrance at monmouthcollege.edu>
 	* wmforecast.c: bugfix -- After downloading the weather, the Weather
 	  struct was being freed, but it contained the pointer to the icon
 	  currently used by the dockapp, causing a crash.  This has been fixed.
 	  In addition, functions have been added to properly free the
 	  Forecast and ForecastArray structs inside each Weather struct.
-	* wmforecast.c, configure.ac: PACKAGE_BUGREPORT now set to my email
-	  address, printed by --help (for the benefit fo help2man)
 	* README: expanded description
 
 2015-04-15  Doug Torrance <dtorrance at monmouthcollege.edu>
@@ -15,5 +22,5 @@
 	  for the benefit of help2man, copyright and author information added
 	  to --version, description and bug report information added to --help
 	* configure.ac, Makefile.am: call help2man during make to automatically
-	  build manpage
+	  build manpagec
 	* configure.ac: set version 0.2
diff --git a/configure b/configure
index 7e13fbd..60d5aa4 100755
--- a/configure
+++ b/configure
@@ -2,6 +2,8 @@
 # Guess values for system-dependent variables and create Makefiles.
 # Generated by GNU Autoconf 2.69 for wmforecast 0.2.
 #
+# Report bugs to <dtorrance at monmouthcollege.edu>.
+#
 #
 # Copyright (C) 1992-1996, 1998-2012 Free Software Foundation, Inc.
 #
@@ -263,7 +265,8 @@ fi
     $as_echo "$0: In particular, zsh $ZSH_VERSION has bugs and should"
     $as_echo "$0: be upgraded to zsh 4.3.4 or later."
   else
-    $as_echo "$0: Please tell bug-autoconf at gnu.org about your system,
+    $as_echo "$0: Please tell bug-autoconf at gnu.org and
+$0: dtorrance at monmouthcollege.edu about your system,
 $0: including any error possibly output before this
 $0: message. Then install a modern shell, or manually run
 $0: the script under such a shell if you do have one."
@@ -578,7 +581,7 @@ PACKAGE_NAME='wmforecast'
 PACKAGE_TARNAME='wmforecast'
 PACKAGE_VERSION='0.2'
 PACKAGE_STRING='wmforecast 0.2'
-PACKAGE_BUGREPORT=''
+PACKAGE_BUGREPORT='dtorrance at monmouthcollege.edu'
 PACKAGE_URL=''
 
 ac_unique_file="configure.ac"
@@ -1343,7 +1346,7 @@ Some influential environment variables:
 Use these variables to override the choices made by `configure' or to help
 it to find libraries and programs with nonstandard names/locations.
 
-Report bugs to the package provider.
+Report bugs to <dtorrance at monmouthcollege.edu>.
 _ACEOF
 ac_status=$?
 fi
@@ -4393,7 +4396,7 @@ $config_headers
 Configuration commands:
 $config_commands
 
-Report bugs to the package provider."
+Report bugs to <dtorrance at monmouthcollege.edu>."
 
 _ACEOF
 cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1
diff --git a/wmforecast.1 b/wmforecast.1
index 2f37187..5bacff0 100644
--- a/wmforecast.1
+++ b/wmforecast.1
@@ -31,7 +31,7 @@ option and it is not guaranteed to work)
 .SH AUTHOR
 Written by Doug Torrance
 .SH "REPORTING BUGS"
-Report bugs to <dtorrance at monmouthcollege.edu>.
+Report bugs to
 .SH COPYRIGHT
 Copyright \(co 2014 Doug Torrance
 License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
diff --git a/wmforecast.c b/wmforecast.c
index e715532..5c389e5 100755
--- a/wmforecast.c
+++ b/wmforecast.c
@@ -13,7 +13,6 @@
 
     You should have received a copy of the GNU General Public License
     along with this program.  If not, see <http://www.gnu.org/licenses/>.
-
 ************************************************************************/
 
 #ifdef HAVE_CONFIG_H
@@ -53,6 +52,8 @@ typedef struct {
 	char *title;
 	ForecastArray *forecasts;
 	RImage *icon;
+	int errorFlag;
+	char *errorText;
 } Weather;
 
 typedef struct {
@@ -101,6 +102,8 @@ Weather *newWeather()
 	weather->title = NULL;
 	weather->icon = NULL;
 	weather->forecasts = newForecastArray();
+	weather->errorFlag = 0;
+	weather->errorText = NULL;
 	return weather;
 }
 
@@ -135,13 +138,11 @@ void freeWeather(Weather *weather)
 		free(weather->text);
 	if (weather->title)
 		free(weather->title);
-	if (weather->forecasts) {
-		if (weather->forecasts->forecasts)
-			free(weather->forecasts->forecasts);
-		free(weather->forecasts);
-	}
-	if (weather)
-		free(weather);
+	if (weather->forecasts)
+		freeForecastArray(weather->forecasts);
+	if (weather->errorText)
+		free(weather->errorText);
+	free(weather);
 }
 
 void setTitle(Weather *weather, const char *title)
@@ -280,6 +281,19 @@ char *getBalloonText(Weather *weather)
 	return text;
 }
 
+void setError(Weather *weather, WMScreen *screen, const char *errorText)
+{
+	weather->errorFlag = 1;
+	weather->errorText = realloc(weather->errorText, strlen(errorText) + 1);
+	strcpy(weather->errorText, errorText);
+
+	
+	
+
+}
+	
+
+
 
 /**************************************************
 from http://curl.haxx.se/libcurl/c/getinmemory.html
@@ -310,34 +324,12 @@ WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
 	return realsize;
 }
 
-struct MemoryStruct downloadWeather(char *url)
+Weather *getWeather(WMScreen *screen, Preferences *prefs)
 {
+	char *url;
 	CURL *curl_handle;
   	CURLcode res;
  	struct MemoryStruct chunk;
-	chunk.memory = malloc(1);
-	chunk.size = 0;
- 
-	curl_global_init(CURL_GLOBAL_ALL);
-	curl_handle = curl_easy_init();
-	curl_easy_setopt(curl_handle, CURLOPT_URL, url);
-	curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
-	curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
-	curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
-	res = curl_easy_perform(curl_handle);
-	if(res != CURLE_OK) {
-		fprintf(stderr, "curl_easy_perform() failed: %s\n",
-			curl_easy_strerror(res));
-	}
-	curl_easy_cleanup(curl_handle);
-	curl_global_cleanup();
-	return chunk;
-}
-
-Weather *getWeather(WMScreen *screen, Preferences *prefs)
-{
-	char *url;
-	struct MemoryStruct chunk;
 	Weather *weather;
 	xmlDocPtr doc;
 	xmlNodePtr cur;
@@ -352,24 +344,43 @@ Weather *getWeather(WMScreen *screen, Preferences *prefs)
 		url = wstrappend(url, prefs->zip);
 	}
 		
-
-
 	weather = newWeather();
-	chunk = downloadWeather(url);
+	chunk.memory = malloc(1);
+	chunk.size = 0;
+ 
+	curl_global_init(CURL_GLOBAL_ALL);
+	curl_handle = curl_easy_init();
+	curl_easy_setopt(curl_handle, CURLOPT_URL, url);
+	curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
+	curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
+	curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
+	res = curl_easy_perform(curl_handle);
+	if(res != CURLE_OK) {
+		setError(weather, screen, curl_easy_strerror(res));
+		curl_easy_cleanup(curl_handle);
+		curl_global_cleanup();
+		if(chunk.memory)
+			free(chunk.memory);
+		return weather;
+	}
+	curl_easy_cleanup(curl_handle);
+	curl_global_cleanup();
 
 	doc = xmlParseMemory(chunk.memory, chunk.size);
 	if (doc == NULL) {
-		fprintf(stderr,"Document not parsed successfully. \n");
+		setError(weather, screen, "Document not parsed successfully");
+		xmlFreeDoc(doc);
 		return;
 	}
 	cur = xmlDocGetRootElement(doc);
 	if (cur == NULL) {
-		fprintf(stderr,"empty document\n");
+		setError(weather, screen,"Empty document");
 		xmlFreeDoc(doc);
 		return;
 	}
 	
 	if (xmlStrcmp(cur->name, (const xmlChar *) "rss")) {
+		setError(weather, screen,"Empty document");
 		fprintf(stderr,"document of the wrong type, root node != rss");
 		xmlFreeDoc(doc);
 		return;
@@ -438,14 +449,30 @@ void updateDockapp(WMScreen *screen, Dockapp *dockapp, Preferences *prefs)
 	WMPixmap *icon;
 
 	weather = getWeather(screen, prefs);
-     
-	WMSetLabelText(dockapp->text,wstrconcat(weather->temp,"°"));
-	
-	icon = WMCreatePixmapFromRImage(screen,weather->icon,0);
-	WMSetLabelImage(dockapp->icon,icon);
+
+	if (weather->errorFlag == 1) {
+		RContext *context;
+
+		WMSetLabelText(dockapp->text,"ERROR");
+
+		context = WMScreenRContext(screen);
+		weather->icon = RLoadImage(context,DATADIR"/na.png",0);
+
+		icon = WMCreatePixmapFromRImage(screen,weather->icon,0);
+		WMSetLabelImage(dockapp->icon,icon);
+
+		WMSetBalloonTextForView(weather->errorText, WMWidgetView(dockapp->icon)); 
+
+	}
+	else {
+		WMSetLabelText(dockapp->text,wstrconcat(weather->temp,"°"));
 	
-	WMSetBalloonTextForView(getBalloonText(weather), WMWidgetView(dockapp->icon)); 
+		icon = WMCreatePixmapFromRImage(screen,weather->icon,0);
+		WMSetLabelImage(dockapp->icon,icon);
 	
+		WMSetBalloonTextForView(getBalloonText(weather), WMWidgetView(dockapp->icon)); 
+	}
+
 	WMRedisplayWidget(dockapp->icon);
 	WMRedisplayWidget(dockapp->text);
 
@@ -517,7 +544,7 @@ Preferences *setPreferences(int argc, char **argv)
 			       "    -z, --zip <zip>     ZIP code or Location ID (Yahoo has deprecated this\n"
 			       "                        option and it is not guaranteed to work)\n"
 			       "(note that only one of -w or -z may be used, not both)\n\n"
-			       "Report bugs to <%s>.\n",
+			       "Report bugs to %s\n",
 			       PACKAGE_BUGREPORT
 				);
 			
@@ -570,9 +597,9 @@ ThreadData *newThreadData(WMScreen *screen, Dockapp *dockapp, Preferences *prefs
 	return data;
 }
 	
-void *timerLoop(void *dataVoid)
+void *timerLoop(void *args)
 {
-	ThreadData *data = dataVoid;
+	ThreadData *data = args;
 	for (;;) {
 		updateDockapp(data->screen, data->dockapp, data->prefs);
 		sleep(60*60);
@@ -583,6 +610,7 @@ int main(int argc, char **argv)
 {
 	Display *display;
 	Dockapp *dockapp;
+	int interval = 5;
 	Preferences *prefs;
 	pthread_t thread;
 	ThreadData *data;
@@ -598,7 +626,7 @@ int main(int argc, char **argv)
 	screen = WMCreateScreen(display, DefaultScreen(display));
 	dockapp = newDockapp(screen, argc, argv);
 	data = newThreadData(screen, dockapp, prefs);
-	
+
 	pthread_create(&thread, NULL, timerLoop, data);
 	WMScreenMainLoop(screen);
 }

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-wmaker/wmforecast.git



More information about the Pkg-wmaker-commits mailing list