From c7f71f3f2b580714bd9e5a5c773bc2179c0e8567 Mon Sep 17 00:00:00 2001 From: louis Date: Thu, 15 Jan 2015 13:12:47 +0100 Subject: Initial commit --- .gitignore | 8 + COPYING | 340 +++++++++++++++++++ HISTORY | 6 + Makefile | 136 ++++++++ README | 65 ++++ config.c | 22 ++ config.h | 20 ++ libforecastio/forecast.c | 151 +++++++++ libforecastio/forecast.h | 101 ++++++ libforecastio/forecastio.c | 173 ++++++++++ libforecastio/forecastio.h | 39 +++ libforecastio/forecasts.c | 120 +++++++ libforecastio/forecasts.h | 34 ++ libforecastio/locator.c | 54 +++ libforecastio/locator.h | 22 ++ libskindesigner/services.h | 52 +++ libskindesigner/skindesignerosdbase.c | 172 ++++++++++ libskindesigner/skindesignerosdbase.h | 62 ++++ po/de_DE.po | 106 ++++++ services.h | 49 +++ setup.c | 47 +++ setup.h | 22 ++ templates/plug-weatherforecast-weatherforecast.xml | 90 +++++ ...eatherforecast-weatherforecastdetailcurrent.xml | 35 ++ ...-weatherforecast-weatherforecastdetaildaily.xml | 40 +++ ...weatherforecast-weatherforecastdetailhourly.xml | 37 ++ templates/weathericons/clear-day.png | Bin 0 -> 4980 bytes templates/weathericons/clear-night.png | Bin 0 -> 3115 bytes templates/weathericons/cloudy.png | Bin 0 -> 6433 bytes templates/weathericons/fog.png | Bin 0 -> 3652 bytes templates/weathericons/partly-cloudy-day.png | Bin 0 -> 7028 bytes templates/weathericons/partly-cloudy-night.png | Bin 0 -> 6409 bytes templates/weathericons/rain.png | Bin 0 -> 6000 bytes templates/weathericons/sleet.png | Bin 0 -> 6021 bytes templates/weathericons/snow.png | Bin 0 -> 7660 bytes templates/weathericons/wind.png | Bin 0 -> 2568 bytes tools/curlfuncs.c | 236 +++++++++++++ tools/curlfuncs.h | 45 +++ tools/filesystem.c | 53 +++ tools/filesystem.h | 14 + tools/jsonhelpers.c | 40 +++ tools/jsonhelpers.h | 17 + weatherforecast.c | 161 +++++++++ weatherosd.c | 371 +++++++++++++++++++++ weatherosd.h | 31 ++ 45 files changed, 2971 insertions(+) create mode 100644 .gitignore create mode 100644 COPYING create mode 100644 HISTORY create mode 100644 Makefile create mode 100644 README create mode 100644 config.c create mode 100644 config.h create mode 100644 libforecastio/forecast.c create mode 100644 libforecastio/forecast.h create mode 100644 libforecastio/forecastio.c create mode 100644 libforecastio/forecastio.h create mode 100644 libforecastio/forecasts.c create mode 100644 libforecastio/forecasts.h create mode 100644 libforecastio/locator.c create mode 100644 libforecastio/locator.h create mode 100644 libskindesigner/services.h create mode 100644 libskindesigner/skindesignerosdbase.c create mode 100644 libskindesigner/skindesignerosdbase.h create mode 100644 po/de_DE.po create mode 100644 services.h create mode 100644 setup.c create mode 100644 setup.h create mode 100644 templates/plug-weatherforecast-weatherforecast.xml create mode 100644 templates/plug-weatherforecast-weatherforecastdetailcurrent.xml create mode 100644 templates/plug-weatherforecast-weatherforecastdetaildaily.xml create mode 100644 templates/plug-weatherforecast-weatherforecastdetailhourly.xml create mode 100644 templates/weathericons/clear-day.png create mode 100644 templates/weathericons/clear-night.png create mode 100644 templates/weathericons/cloudy.png create mode 100644 templates/weathericons/fog.png create mode 100644 templates/weathericons/partly-cloudy-day.png create mode 100644 templates/weathericons/partly-cloudy-night.png create mode 100644 templates/weathericons/rain.png create mode 100644 templates/weathericons/sleet.png create mode 100644 templates/weathericons/snow.png create mode 100644 templates/weathericons/wind.png create mode 100644 tools/curlfuncs.c create mode 100644 tools/curlfuncs.h create mode 100644 tools/filesystem.c create mode 100644 tools/filesystem.h create mode 100644 tools/jsonhelpers.c create mode 100644 tools/jsonhelpers.h create mode 100644 weatherforecast.c create mode 100644 weatherosd.c create mode 100644 weatherosd.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..19526e0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +.dependencies +*.o +*.so +*.orig +*.rej +*.mo +*.pot +*.tgz diff --git a/COPYING b/COPYING new file mode 100644 index 0000000..f90922e --- /dev/null +++ b/COPYING @@ -0,0 +1,340 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc. + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Lesser General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) year name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. diff --git a/HISTORY b/HISTORY new file mode 100644 index 0000000..7bfa408 --- /dev/null +++ b/HISTORY @@ -0,0 +1,6 @@ +VDR Plugin 'weatherforecast' Revision History +--------------------------------------------- + +2015-01-15: Version 0.0.1 + +- Initial revision. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..fba7ca0 --- /dev/null +++ b/Makefile @@ -0,0 +1,136 @@ +# +# Makefile for a Video Disk Recorder plugin +# +# $Id$ + +# The official name of this plugin. +# This name will be used in the '-P...' option of VDR to load the plugin. +# By default the main source file also carries this name. + +PLUGIN = weatherforecast + +### The version number of this plugin (taken from the main source file): + +VERSION = $(shell grep 'static const char \*VERSION *=' $(PLUGIN).c | awk '{ print $$6 }' | sed -e 's/[";]//g') + +### The directory environment: + +# Use package data if installed...otherwise assume we're under the VDR source directory: +PKGCFG = $(if $(VDRDIR),$(shell pkg-config --variable=$(1) $(VDRDIR)/vdr.pc),$(shell PKG_CONFIG_PATH="$$PKG_CONFIG_PATH:../../.." pkg-config --variable=$(1) vdr)) +LIBDIR = $(call PKGCFG,libdir) +LOCDIR = $(call PKGCFG,locdir) +PLGCFG = $(call PKGCFG,plgcfg) +# +TMPDIR ?= /tmp + +### The compiler options: + +export CFLAGS = $(call PKGCFG,cflags) +export CXXFLAGS = $(call PKGCFG,cxxflags) + +### The version number of VDR's plugin API: + +APIVERSION = $(call PKGCFG,apiversion) + +### Allow user defined options to overwrite defaults: + +-include $(PLGCFG) + +### The name of the distribution archive: + +ARCHIVE = $(PLUGIN)-$(VERSION) +PACKAGE = vdr-$(ARCHIVE) + +### The name of the shared object file: + +SOFILE = libvdr-$(PLUGIN).so + +### Includes and Defines (add further entries here): + +INCLUDES += + +DEFINES += -DPLUGIN_NAME_I18N='"$(PLUGIN)"' + +LIBS += $(shell pkg-config --libs libcurl) +LIBS += $(shell pkg-config --cflags --libs jansson) + +### The object files (add further files here): + +OBJS = $(PLUGIN).o \ + config.o \ + setup.o \ + libskindesigner/skindesignerosdbase.o \ + tools/curlfuncs.o \ + tools/jsonhelpers.o \ + tools/filesystem.o \ + libforecastio/forecastio.o \ + libforecastio/forecasts.o \ + libforecastio/forecast.o \ + libforecastio/locator.o \ + weatherosd.o + +### The main target: + +all: $(SOFILE) i18n + +### Implicit rules: + +%.o: %.c + $(CXX) $(CXXFLAGS) -c $(DEFINES) $(INCLUDES) -o $@ $< + +### Dependencies: + +MAKEDEP = $(CXX) -MM -MG +DEPFILE = .dependencies +$(DEPFILE): Makefile + @$(MAKEDEP) $(CXXFLAGS) $(DEFINES) $(INCLUDES) $(OBJS:%.o=%.c) > $@ + +-include $(DEPFILE) + +### Internationalization (I18N): + +PODIR = po +I18Npo = $(wildcard $(PODIR)/*.po) +I18Nmo = $(addsuffix .mo, $(foreach file, $(I18Npo), $(basename $(file)))) +I18Nmsgs = $(addprefix $(DESTDIR)$(LOCDIR)/, $(addsuffix /LC_MESSAGES/vdr-$(PLUGIN).mo, $(notdir $(foreach file, $(I18Npo), $(basename $(file)))))) +I18Npot = $(PODIR)/$(PLUGIN).pot + +%.mo: %.po + msgfmt -c -o $@ $< + +$(I18Npot): $(wildcard *.c) + xgettext -C -cTRANSLATORS --no-wrap --no-location -k -ktr -ktrNOOP --package-name=vdr-$(PLUGIN) --package-version=$(VERSION) --msgid-bugs-address='' -o $@ `ls $^` + +%.po: $(I18Npot) + msgmerge -U --no-wrap --no-location --backup=none -q -N $@ $< + @touch $@ + +$(I18Nmsgs): $(DESTDIR)$(LOCDIR)/%/LC_MESSAGES/vdr-$(PLUGIN).mo: $(PODIR)/%.mo + install -D -m644 $< $@ + +.PHONY: i18n +i18n: $(I18Nmo) $(I18Npot) + +install-i18n: $(I18Nmsgs) + +### Targets: + +$(SOFILE): $(OBJS) + $(CXX) $(CXXFLAGS) $(LDFLAGS) -shared $(OBJS) $(LIBS) -o $@ + +install-lib: $(SOFILE) + install -D $^ $(DESTDIR)$(LIBDIR)/$^.$(APIVERSION) + +install: install-lib install-i18n + +dist: $(I18Npo) clean + @-rm -rf $(TMPDIR)/$(ARCHIVE) + @mkdir $(TMPDIR)/$(ARCHIVE) + @cp -a * $(TMPDIR)/$(ARCHIVE) + @tar czf $(PACKAGE).tgz -C $(TMPDIR) $(ARCHIVE) + @-rm -rf $(TMPDIR)/$(ARCHIVE) + @echo Distribution package created as $(PACKAGE).tgz + +clean: + @-rm -f $(PODIR)/*.mo $(PODIR)/*.pot + @-rm -f $(OBJS) $(DEPFILE) *.so *.tgz core* *~ diff --git a/README b/README new file mode 100644 index 0000000..2b83531 --- /dev/null +++ b/README @@ -0,0 +1,65 @@ +This is a "plugin" for the Video Disk Recorder (VDR). + +Written by: Louis Braun + +Project's homepage: http://projects.vdr-developer.org/projects/plg-weatherforecast + +Latest version available at: http://projects.vdr-developer.org/git/vdr-plugin-weatherforecast.git/ + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. +See the file COPYING for more information. + +Description: +------------ + +WeatherForecast provides a weather forecast (who'd have thought? ;) ) based on +forecast.io data. + +Requirements: +------------- +- VDR 1.7.x +- libcurl +- jansson + +Installation, configuration: +---------------------------- + +To download the plugin from the GIT repository, do a + +git clone git://projects.vdr-developer.org/vdr-plugin-weatherforecast.git + +Install the plugin depending on your used distribution. + +The plugin has no startup parameters. During first startup of the plugin +(and everytime the VDR setup parameter "weatherforecast.city" is empty) +automatically an online lookup is done to detect your location (city, longitude, +latitude) by your used public ip. The latitude and longitude results are used +by the plugin to fetch appropriate data from forecast.io. The city is just +used for displaying purposes. + +Since weatherforecast.io allows only 1000 API calls per day for free, the results +from forecast.io are cached up to 20h in + +/weatherforecast/weather.json + +If this file does not exist on Plugin startup, a new online lookup is done anyway. + +city, longitude and latitude are stored and can be adapted manually in the plugin +setup menu. f you change latitude or longitude, just delete the cached +weather.json file and restart VDR to achieve new data for the newly configured +location. + +Usage: +------ + +The plugin provides three different menus: "current weather", "next 48 hours" +and "next 7 days". Additionally the plugin provides a service interface so that +skins and other plugins are able to achieve weather data from the plugin. + +Even if the plugin runs with every skin, best results can be reached by using a +skindesigner skin which implements the weather plugin templates. + + diff --git a/config.c b/config.c new file mode 100644 index 0000000..e7b2293 --- /dev/null +++ b/config.c @@ -0,0 +1,22 @@ +#include "config.h" + +cWeatherforecastConfig::cWeatherforecastConfig() { + //number of hours to wait till next update from forecast.io is fetched + hoursToUpdate = 20; + //city to display in menus + city = ""; + //latitude and longitude of location for forecast.io query + lat = 0.0; + lon = 0.0; +} + +cWeatherforecastConfig::~cWeatherforecastConfig() { +} + +bool cWeatherforecastConfig::SetupParse(const char *Name, const char *Value) { + if (!strcasecmp(Name, "city")) city = Value; + else if (!strcasecmp(Name, "lat")) lat = atod(Value); + else if (!strcasecmp(Name, "lon")) lon = atod(Value); + else return false; + return true; +} diff --git a/config.h b/config.h new file mode 100644 index 0000000..6adc3fb --- /dev/null +++ b/config.h @@ -0,0 +1,20 @@ +#ifndef __WEATHERFORECAST_CONFIG_H +#define __WEATHERFORECAST_CONFIG_H + +#include +#include + +using namespace std; + +class cWeatherforecastConfig { +private: +public: + cWeatherforecastConfig(); + ~cWeatherforecastConfig(); + bool SetupParse(const char *Name, const char *Value); + int hoursToUpdate; + string city; + float lat; + float lon; +}; +#endif //__WEATHERFORECAST_CONFIG_H diff --git a/libforecastio/forecast.c b/libforecastio/forecast.c new file mode 100644 index 0000000..db09e36 --- /dev/null +++ b/libforecastio/forecast.c @@ -0,0 +1,151 @@ +#include +#include +#include +#include +#include "../tools/jsonhelpers.h" +#include "forecast.h" + +using namespace std; + +cForecast::cForecast(void) { + type = ftUnknownForecast; + forecastTime = 0; + summary = ""; + icon = ""; + sunriseTime = 0; + sunsetTime = 0; + moonPhase = 0.0; + precipIntensity = 0.0; + precipProbability = 0.0; + percipType = ""; + temperature = 0.0; + apparentTemperature = 0.0; + temperatureMin = 0.0; + temperatureMinTime = 0; + temperatureMax = 0.0; + temperatureMaxTime = 0; + humidity = 0.0; + windSpeed = 0.0; + windBearing = 0; + visibility = 0.0; + cloudCover = 0.0; + pressure = 0.0; + ozone = 0.0; +} + +cForecast::~cForecast() { +} + +void cForecast::SetForecast(json_t *forecast) { + forecastTime = JsonGetInteger(forecast, "time"); + summary = JsonGetString(forecast, "summary"); + icon = JsonGetString(forecast, "icon"); + sunriseTime = JsonGetInteger(forecast, "sunriseTime"); + sunsetTime = JsonGetInteger(forecast, "sunsetTime"); + moonPhase = JsonGetFloat(forecast, "moonPhase"); + precipIntensity = JsonGetFloat(forecast, "precipIntensity"); + precipProbability = JsonGetFloat(forecast, "precipProbability"); + percipType = JsonGetString(forecast, "percipType"); + temperature = JsonGetFloat(forecast, "temperature"); + apparentTemperature = JsonGetFloat(forecast, "apparentTemperature"); + temperatureMin = JsonGetFloat(forecast, "temperatureMin"); + temperatureMinTime = JsonGetInteger(forecast, "temperatureMinTime"); + temperatureMax = JsonGetFloat(forecast, "temperatureMax"); + temperatureMaxTime = JsonGetInteger(forecast, "temperatureMaxTime"); + humidity = JsonGetFloat(forecast, "humidity"); + windSpeed = JsonGetFloat(forecast, "windSpeed"); + windBearing = JsonGetInteger(forecast, "windBearing"); + visibility = JsonGetFloat(forecast, "visibility"); + cloudCover = JsonGetFloat(forecast, "cloudCover"); + pressure = JsonGetFloat(forecast, "pressure"); + ozone = JsonGetFloat(forecast, "ozone"); +} + +bool cForecast::TimeMatch(time_t compare) { + //forecast is valid if time of forecast is between 30mins before or after compare time + int difference = abs(compare - forecastTime); + if (difference <= 1800) + return true; + return false; +} + +bool cForecast::DayMatch(time_t compare) { + int difference = compare - forecastTime; + if (difference >= 0 && difference <= 86400) + return true; + return false; +} + +string cForecast::GetDate(time_t myTime) { + struct tm *ts; + ts = localtime(&myTime); + return *cString::sprintf("%d.%02d", ts->tm_mday, ts->tm_mon+1); +} + +string cForecast::GetTime(time_t myTime) { + struct tm *ts; + ts = localtime(&myTime); + return *cString::sprintf("%02d:%02d", ts->tm_hour, ts->tm_min); +} + +string cForecast::GetDateTime(time_t myTime) { + struct tm *ts; + ts = localtime(&myTime); + return *cString::sprintf("%d.%02d %02d:%02d", ts->tm_mday, ts->tm_mon+1, ts->tm_hour, ts->tm_min); +} + +string cForecast::GetWindBearingString(void) { + string sBearing = ""; + if (windBearing >= 349 || windBearing <= 11) { + sBearing = "N"; + } else if (windBearing > 11 && windBearing <= 34) { + sBearing = "NNE"; + } else if (windBearing > 34 && windBearing <= 56) { + sBearing = "NE"; + } else if (windBearing > 56 && windBearing <= 79) { + sBearing = "ENE"; + } else if (windBearing > 79 && windBearing <= 101) { + sBearing = "E"; + } else if (windBearing > 101 && windBearing <= 123) { + sBearing = "ESE"; + } else if (windBearing > 123 && windBearing <= 145) { + sBearing = "SE"; + } else if (windBearing > 145 && windBearing <= 168) { + sBearing = "SSE"; + } else if (windBearing > 168 && windBearing <= 191) { + sBearing = "S"; + } else if (windBearing > 191 && windBearing <= 213) { + sBearing = "SSW"; + } else if (windBearing > 213 && windBearing <= 235) { + sBearing = "SW"; + } else if (windBearing > 235 && windBearing <= 258) { + sBearing = "WSW"; + } else if (windBearing > 258 && windBearing <= 281) { + sBearing = "W"; + } else if (windBearing > 281 && windBearing <= 303) { + sBearing = "WNW"; + } else if (windBearing > 303 && windBearing <= 325) { + sBearing = "NW"; + } else if (windBearing > 325 && windBearing <= 348) { + sBearing = "NNW"; + } + return sBearing; +} + +void cForecast::Debug(bool verbose) { + if (type == ftCurrent) { + dsyslog("weatherforecast: current weather for %s at %s, summary: \"%s\", icon: \"%s\"", GetDate(forecastTime).c_str(), GetTime(forecastTime).c_str(), summary.c_str(), icon.c_str()); + } else if (type == ftHourly) { + dsyslog("weatherforecast: hourly weather for %s on %s, summary: \"%s\", icon: \"%s\"", GetDate(forecastTime).c_str(), GetTime(forecastTime).c_str(), summary.c_str(), icon.c_str()); + } else if (type == ftDaily) { + dsyslog("weatherforecast: daily weather for %s, summary: \"%s\", icon: \"%s\"", GetDate(forecastTime).c_str(), summary.c_str(), icon.c_str()); + } + + if (!verbose) + return; + dsyslog("weatherforecast: sunrise time %ld, sunset time: %ld, moon phase %f", sunriseTime, sunsetTime, moonPhase); + dsyslog("weatherforecast: min temp %f at %ld, max temp %f at %ld", temperatureMin, temperatureMinTime, temperatureMax, temperatureMaxTime); + dsyslog("weatherforecast: precipInt %f, precipProp: %f, type %s", precipIntensity, precipProbability, percipType.c_str()); + dsyslog("weatherforecast: temperature %f °C, apparentTemp %f °C, humidity: %f", temperature, apparentTemperature, humidity); + dsyslog("weatherforecast: windSpeed %f, windBearing: %d, visibility %f, cloudCover: %f, pressure %f, ozone: %f", windSpeed, windBearing, visibility, cloudCover, pressure, ozone); +} diff --git a/libforecastio/forecast.h b/libforecastio/forecast.h new file mode 100644 index 0000000..ccb250d --- /dev/null +++ b/libforecastio/forecast.h @@ -0,0 +1,101 @@ +#ifndef __FORECAST_H +#define __FORECAST_H + +#include +#include + +using namespace std; + +enum eForecastType { + ftUnknownForecast, + ftCurrent, + ftHourly, + ftDaily, +}; + +class cForecast { +private: + eForecastType type; + time_t forecastTime; + string summary; + string icon; + time_t sunriseTime; + time_t sunsetTime; + float moonPhase; + float precipIntensity; + float precipProbability; + string percipType; + float temperature; + float apparentTemperature; + float temperatureMin; + time_t temperatureMinTime; + float temperatureMax; + time_t temperatureMaxTime; + float humidity; + float windSpeed; + int windBearing; + float visibility; + float cloudCover; + float pressure; + float ozone; + string GetDate(time_t myTime); + string GetTime(time_t myTime); + string GetDateTime(time_t myTime); +public: + cForecast(void); + virtual ~cForecast(void); + void SetForecast(json_t *forecast); + void SetForecastType(eForecastType type) { this->type = type; }; + bool TimeMatch(time_t compare); + bool DayMatch(time_t compare); + void Debug(bool verbose = false); + //Getter Functions for native access to data + time_t GetTime(void) { return forecastTime; }; + string GetSummary(void) { return summary; }; + string GetIcon(void) { return icon; }; + time_t GetSunrise(void) { return sunriseTime; }; + time_t GetSunset(void) { return sunsetTime; }; + float GetMoonphase(void) { return moonPhase; }; + float GetPrecipIntensity(void) { return precipIntensity; }; + float GetPrecipProbability(void) { return precipProbability; }; + string GetPercipType(void) { return percipType; }; + float GetTemperature(void) { return temperature; }; + float GetTemperatureMin(void) { return temperatureMin; }; + time_t GetTemperatureMinTime(void) { return temperatureMinTime; }; + float GetTemperatureMax(void) { return temperatureMax; }; + time_t GetTemperatureMaxTime(void) { return temperatureMaxTime; }; + float GetApparentTemperature(void) { return apparentTemperature; }; + float GetHumidity(void) { return humidity; }; + float GetWindSpeed(void) { return windSpeed; }; + int GetWindBearing(void) { return windBearing; }; + float GetVisibility(void) { return visibility; }; + float GetCloudCover(void) { return cloudCover; }; + float GetPressure(void) { return pressure; }; + float GetOzone(void) { return ozone; }; + //convenient Getter Functions + string GetDateTimeString(void) { return GetDateTime(forecastTime); }; + string GetDateString(void) { return GetDate(forecastTime); }; + string GetTimeString(void) { return GetTime(forecastTime); }; + string GetDayName(void) { return *WeekDayName(forecastTime); }; + string GetSunriseString(void) { return GetTime(sunriseTime); }; + string GetSunsetString(void) { return GetTime(sunsetTime); }; + int GetMoonphasePercent(void) { return (int)(moonPhase*100); }; + string GetPrecipIntensityString(void) { return *cString::sprintf("%.1f", precipIntensity); }; + int GetPrecipProbabilityPercent(void) { return (int)(precipProbability*100); }; + string GetTemperatureString(void) { return *cString::sprintf("%.1f", temperature); }; + string GetTemperatureMinString(void) { return *cString::sprintf("%.1f", temperatureMin); }; + string GetTemperatureMinTimeString(void) { return GetTime(temperatureMinTime); }; + string GetTemperatureMaxString(void) { return *cString::sprintf("%.1f", temperatureMax); }; + string GetTemperatureMaxTimeString(void) { return GetTime(temperatureMaxTime); }; + string GetApparentTemperatureString(void) { return *cString::sprintf("%.1f", apparentTemperature); }; + int GetHumidityPercent(void) { return (int)(humidity*100); }; + string GetWindSpeedString(void) { return *cString::sprintf("%.1f", windSpeed); }; + string GetWindBearingString(void); + string GetVisibilityString(void) { return *cString::sprintf("%.1f", visibility); }; + int GetCloudCoverPercent(void) { return (int)(cloudCover*100); }; + string GetPressureString(void) { return *cString::sprintf("%.1f", pressure); }; + string GetOzoneString(void) { return *cString::sprintf("%.1f", ozone); }; +}; + + +#endif //__FORECASTIO_H \ No newline at end of file diff --git a/libforecastio/forecastio.c b/libforecastio/forecastio.c new file mode 100644 index 0000000..1a8db4e --- /dev/null +++ b/libforecastio/forecastio.c @@ -0,0 +1,173 @@ +#include +#include +#include +#include +#include "../config.h" +#include "../tools/curlfuncs.h" +#include "../tools/filesystem.h" +#include "forecastio.h" + +using namespace std; +extern cWeatherforecastConfig weatherConfig; + +cForecastIO::cForecastIO(string cacheDir) { + this->cacheDir = cacheDir; + ok = false; + cacheDuration = 60 * 60 * weatherConfig.hoursToUpdate; + apiKey = "9830052ef63efbec84ec0639e9a205d2"; + string osdLanguage = Setup.OSDLanguage; + language = osdLanguage.substr(0,2); + unit = "si"; + baseURL = "https://api.forecast.io/forecast"; + latitude = weatherConfig.lat; + longitude = weatherConfig.lon; + current = NULL; + hourly = NULL; + daily = NULL; +} + +cForecastIO::~cForecastIO() { + if (current) + delete current; + if (hourly) + delete hourly; + if (daily) + delete daily; +} + +/***************************************************************** +* PUBLIC FUNCTIONS +*****************************************************************/ +void cForecastIO::Action(void) { + ok = ReadForecast(); +} + +cForecast *cForecastIO::GetCurrentForecast(void) { + time_t now = time(0); + if (current && current->TimeMatch(now)) + return current; + if (hourly) + return hourly->GetCurrent(); + return NULL; +} + +bool cForecastIO::SetCurrentWeather(cServiceCurrentWeather *call) { + cForecast *currentForecast = GetCurrentForecast(); + if (!currentForecast) + return false; + call->timeStamp = currentForecast->GetDateTimeString(); + call->temperature = currentForecast->GetTemperatureString(); + call->apparentTemperature = currentForecast->GetApparentTemperatureString(); + call->summary = currentForecast->GetSummary(); + call->icon = currentForecast->GetIcon(); + call->precipitationIntensity = currentForecast->GetPrecipIntensityString(); + call->precipitationProbability = currentForecast->GetPrecipProbabilityPercent(); + call->precipitationType = currentForecast->GetPercipType(); + call->humidity = currentForecast->GetHumidityPercent(); + call->windSpeed = currentForecast->GetWindSpeedString(); + call->windBearing = currentForecast->GetWindBearing(); + call->windBearingString = currentForecast->GetWindBearingString(); + call->visibility = currentForecast->GetVisibilityString(); + call->cloudCover = currentForecast->GetCloudCoverPercent(); + call->pressure = currentForecast->GetPressureString(); + call->ozone = currentForecast->GetOzoneString(); + + cForecast *dailyForecast = daily->GetFirstDaily(); + if (!dailyForecast) + return true; + call->minTemperature = dailyForecast->GetTemperatureMinString(); + call->maxTemperature = dailyForecast->GetTemperatureMaxString(); + + return true; +} + +/***************************************************************** +* PRIVATE FUNCTIONS +*****************************************************************/ +bool cForecastIO::ReadForecast(void) { + string forecastFile = *cString::sprintf("%s/%s", cacheDir.c_str(), "weather.json"); + dsyslog("weatherforecast: trying to read cached forecast from %s", forecastFile.c_str()); + string forecastJson = ""; + if (!FileExists(forecastFile)) { + //get new from forecast.io + dsyslog("weatherforecast: no cached forecast, fetching newly from forecast.io"); + forecastJson = FetchOnlineForecast(); + WriteIntoFile(forecastFile, forecastJson); + } else { + //check if cached file is too old + time_t fileCreation = FileCreationTime(forecastFile); + time_t now = time(0); + int age = now - fileCreation; + int ageHours = age / 3600; + int ageMinutes = (age%3600) / 60; + if (age > cacheDuration) { + //get new from forecast.io + dsyslog("weatherforecast: cached forecast is with %dh %dmin too old, fetching newly from forecast.io", ageHours, ageMinutes); + forecastJson = FetchOnlineForecast(); + WriteIntoFile(forecastFile, forecastJson); + } else { + //using cached data + dsyslog("weatherforecast: cached forecast is only %dh %dmin old, using cached forecast", ageHours, ageMinutes); + forecastJson = ReadFromFile(forecastFile); + } + } + ParseForecast(forecastJson); + + return true; +} + +string cForecastIO::FetchOnlineForecast(void) { + stringstream url; + url << baseURL << "/" << apiKey; + url << "/" << latitude << "," << longitude; + url << "?lang=" << language << "&units=" << unit; + string outputForecastIO; + esyslog("weatherforecast: calling URL %s", url.str().c_str()); + CurlGetUrl(url.str().c_str(), &outputForecastIO); + return outputForecastIO; +} + +bool cForecastIO::ParseForecast(string &jsonForecast) { + json_t *root; + json_error_t error; + + root = json_loads(jsonForecast.c_str(), 0, &error); + if ( !root ) { + return false; + } + if ( !json_is_object(root) ) { + return false; + } + + //reading current weather + json_t *currentWeather = json_object_get(root, "currently"); + if (!json_is_object(currentWeather)) { + return false; + } + current = new cForecast(); + current->SetForecastType(ftCurrent); + current->SetForecast(currentWeather); + //current->Debug(); + + //reading 48 hour forecast + json_t *hourlyWeather = json_object_get(root, "hourly"); + if (!json_is_object(hourlyWeather)) { + return false; + } + hourly = new cForecasts(); + hourly->SetForecastType(ftHourly); + hourly->SetForecast(hourlyWeather); + //hourly->Debug(); + + //reading 7 day forecast + json_t *dailyWeather = json_object_get(root, "daily"); + if (!json_is_object(dailyWeather)) { + return false; + } + daily = new cForecasts(); + daily->SetForecastType(ftDaily); + daily->SetForecast(dailyWeather); + //daily->Debug(); + + return true; +} diff --git a/libforecastio/forecastio.h b/libforecastio/forecastio.h new file mode 100644 index 0000000..c016fb2 --- /dev/null +++ b/libforecastio/forecastio.h @@ -0,0 +1,39 @@ +#ifndef __FORECASTIO_H +#define __FORECASTIO_H + +#include +#include "forecasts.h" +#include "forecast.h" +#include "../services.h" + +using namespace std; + +class cForecastIO : public cThread { +private: + bool ok; + string cacheDir; + int cacheDuration; + string apiKey; + string language; + string unit; + string baseURL; + double latitude; + double longitude; + cForecast *current; + cForecasts *hourly; + cForecasts *daily; + string FetchOnlineForecast(void); + bool ParseForecast(string &jsonForecast); + void Action(void); + bool ReadForecast(void); +public: + cForecastIO(string cacheDir); + virtual ~cForecastIO(void); + cForecast *GetCurrentForecast(void); + cForecasts *GetHourlyForecast(void) { return hourly; }; + cForecasts *GetDailyForecast(void) { return daily; }; + bool SetCurrentWeather(cServiceCurrentWeather *call); +}; + + +#endif //__FORECASTIO_H \ No newline at end of file diff --git a/libforecastio/forecasts.c b/libforecastio/forecasts.c new file mode 100644 index 0000000..d7cb908 --- /dev/null +++ b/libforecastio/forecasts.c @@ -0,0 +1,120 @@ +#include +#include +#include +#include +#include "../tools/jsonhelpers.h" +#include "forecasts.h" + +using namespace std; + +cForecasts::cForecasts(void) { + type = ftUnknownForecast; + summary = ""; + icon = ""; + numDataPoints = 0; + dataPointPointer = 0; + dataPoints = NULL; +} + +cForecasts::~cForecasts() { + if (dataPoints && numDataPoints > 0) { + for (int i=0; iSetForecast(jDataPoint); + dataPoint->SetForecastType(type); + dataPoints[i] = dataPoint; + } +} + +cForecast *cForecasts::GetForecast(int dataPoint) { + if (!dataPoints) + return NULL; + if (dataPoint < 0 || dataPoint >= numDataPoints) + return NULL; + return dataPoints[dataPoint]; +} + +cForecast *cForecasts::GetCurrent(void) { + if (!dataPoints) + return NULL; + time_t now = time(0); + for (int i=0; iTimeMatch(now)) + return dataPoints[i]; + } + return NULL; +} + +cForecast *cForecasts::GetFirstHourly(void) { + if (!dataPoints) + return NULL; + time_t now = time(0); + for (int i=0; iTimeMatch(now)) { + dataPointPointer = i; + return dataPoints[dataPointPointer]; + } + } + return NULL; +} + +cForecast *cForecasts::GetFirstDaily(void) { + if (!dataPoints) + return NULL; + time_t now = time(0); + for (int i=0; iDayMatch(now)) { + dataPointPointer = i; + return dataPoints[dataPointPointer]; + } + } + return NULL; +} + + +cForecast *cForecasts::GetNext(void) { + if (!dataPoints) + return NULL; + dataPointPointer++; + if (dataPointPointer < numDataPoints) { + return dataPoints[dataPointPointer]; + } + dataPointPointer = 0; + return NULL; +} + +void cForecasts::Debug(void) { + if (type == ftHourly) { + dsyslog("weatherforecast: 48 hour forecast"); + } else if (type == ftDaily) { + dsyslog("weatherforecast: 7 day forecast"); + } + dsyslog("weatherforecast: summary: \"%s\"", summary.c_str()); + dsyslog("weatherforecast: icon: \"%s\"", icon.c_str()); + for (int i = 0; i < numDataPoints; i++) { + dsyslog("weatherforecast: ------------ %s %d -----------", (type == ftHourly)?"Hour":"Day", i); + dataPoints[i]->Debug(); + } +} diff --git a/libforecastio/forecasts.h b/libforecastio/forecasts.h new file mode 100644 index 0000000..9730823 --- /dev/null +++ b/libforecastio/forecasts.h @@ -0,0 +1,34 @@ +#ifndef __FORECASTS_H +#define __FORECASTS_H + +#include +#include +#include "forecast.h" + +using namespace std; + +class cForecasts { +private: + eForecastType type; + string summary; + string icon; + int numDataPoints; + int dataPointPointer; + cForecast **dataPoints; +public: + cForecasts(void); + virtual ~cForecasts(void); + void SetForecast(json_t *forecast); + void SetForecastType(eForecastType type) { this->type = type; }; + string GetSummary(void) { return summary; }; + string GetIcon(void) { return icon; }; + cForecast *GetForecast(int dataPoint); + cForecast *GetCurrent(void); + cForecast *GetFirstHourly(void); + cForecast *GetFirstDaily(void); + cForecast *GetNext(void); + void Debug(void); +}; + + +#endif //__FORECASTS_H \ No newline at end of file diff --git a/libforecastio/locator.c b/libforecastio/locator.c new file mode 100644 index 0000000..ef2216d --- /dev/null +++ b/libforecastio/locator.c @@ -0,0 +1,54 @@ +#include +#include +#include "../tools/curlfuncs.h" +#include "../tools/jsonhelpers.h" +#include "locator.h" + +using namespace std; + +cForecastLocator::cForecastLocator(void) { + urlIPLApi = "http://ip-api.com/json"; + city = ""; + lat = 0.0; + lon = 0.0; +} + +cForecastLocator::~cForecastLocator() { + +} + +bool cForecastLocator::ReadLocationByIP(void) { + string outputJson; + CurlGetUrl(urlIPLApi.c_str(), &outputJson); + + json_t *root; + json_error_t error; + + root = json_loads(outputJson.c_str(), 0, &error); + if ( !root ) { + return false; + } + if ( !json_is_object(root) ) { + return false; + } + + city = JsonGetString(root, "city"); + lat = JsonGetFloat(root, "lat"); + lon = JsonGetFloat(root, "lon"); + + dsyslog("weatherforecast: location read from IP: city \"%s\", latitude: %f, longitude: %f", city.c_str(), lat, lon); + + return true; +} + +void cForecastLocator::WriteToSetup(cPlugin *weatherPlug) { + //save to setup + weatherPlug->SetupStore("city", city.c_str()); + weatherPlug->SetupStore("lat", FloatToString(lat).c_str()); + weatherPlug->SetupStore("lon", FloatToString(lon).c_str()); + //set values in plugin config + weatherPlug->SetupParse("city", city.c_str()); + weatherPlug->SetupParse("lat", FloatToString(lat).c_str()); + weatherPlug->SetupParse("lon", FloatToString(lon).c_str()); +} + diff --git a/libforecastio/locator.h b/libforecastio/locator.h new file mode 100644 index 0000000..b29637e --- /dev/null +++ b/libforecastio/locator.h @@ -0,0 +1,22 @@ +#ifndef __FORECASTLOCATOR_H +#define __FORECASTLOCATOR_H + +#include + +using namespace std; + +class cForecastLocator { +private: + string urlIPLApi; + string city; + float lat; + float lon; +public: + cForecastLocator(void); + virtual ~cForecastLocator(void); + bool ReadLocationByIP(void); + void WriteToSetup(cPlugin *weatherPlug); +}; + + +#endif //__FORECASTLOCATOR_H \ No newline at end of file diff --git a/libskindesigner/services.h b/libskindesigner/services.h new file mode 100644 index 0000000..0a016fa --- /dev/null +++ b/libskindesigner/services.h @@ -0,0 +1,52 @@ +#ifndef __SKINDESIGNERSERVICES_H +#define __SKINDESIGNERSERVICES_H + +using namespace std; + +#include +#include +#include + +enum eMenuType { + mtList, + mtText +}; + +class cSDDisplayMenu : public cSkinDisplayMenu { +public: + virtual void SetTitle(const char *Title); + virtual void SetPluginMenu(string name, int menu, int type, bool init); + virtual bool SetItemPlugin(map *stringTokens, map *intTokens, map > > *loopTokens, int Index, bool Current, bool Selectable); + virtual bool SetPluginText(map *stringTokens, map *intTokens, map > > *loopTokens); +}; + +/********************************************************************* +* Data Structures for Service Calls +*********************************************************************/ + +// Data structure for service "RegisterPlugin" +class RegisterPlugin { +public: + RegisterPlugin(void) { + name = ""; + }; + void SetMenu(int key, string templateName) { + menus.insert(pair(key, templateName)); + } +// in + string name; //name of plugin + map< int, string > menus; //menus as key -> templatename hashmap +//out +}; + +// Data structure for service "GetDisplayMenu" +class GetDisplayMenu { +public: + GetDisplayMenu(void) { + displayMenu = NULL; + }; +// in +//out + cSDDisplayMenu *displayMenu; +}; +#endif //__SKINDESIGNERSERVICES_H \ No newline at end of file diff --git a/libskindesigner/skindesignerosdbase.c b/libskindesigner/skindesignerosdbase.c new file mode 100644 index 0000000..821efdc --- /dev/null +++ b/libskindesigner/skindesignerosdbase.c @@ -0,0 +1,172 @@ +#include "skindesignerosdbase.h" + +/********************************************************************** +* cSkindesignerOsdItem +**********************************************************************/ +cSkindesignerOsdItem::cSkindesignerOsdItem(eOSState State) : cOsdItem(State) { + sdDisplayMenu = NULL; +} + +cSkindesignerOsdItem::cSkindesignerOsdItem(const char *Text, eOSState State, bool Selectable) : cOsdItem(Text, State, Selectable) { + sdDisplayMenu = NULL; +} + +cSkindesignerOsdItem::~cSkindesignerOsdItem() { + +} + +void cSkindesignerOsdItem::SetMenuItem(cSkinDisplayMenu *DisplayMenu, int Index, bool Current, bool Selectable) { + if (sdDisplayMenu) { + if (!sdDisplayMenu->SetItemPlugin(&stringTokens, &intTokens, &loopTokens, Index, Current, Selectable)) { + DisplayMenu->SetItem(Text(), Index, Current, Selectable); + } + } else { + DisplayMenu->SetItem(Text(), Index, Current, Selectable); + } +} + +void cSkindesignerOsdItem::AddStringToken(string key, string value) { + stringTokens.insert(pair(key, value)); +} + +void cSkindesignerOsdItem::AddIntToken(string key, int value) { + intTokens.insert(pair(key, value)); +} + +void cSkindesignerOsdItem::AddLoopToken(string loopName, map &tokens) { + map > >::iterator hitLoop = loopTokens.find(loopName); + if (hitLoop == loopTokens.end()) { + vector > tokenVector; + tokenVector.push_back(tokens); + loopTokens.insert(pair > >(loopName, tokenVector)); + } else { + vector > *tokenVector = &hitLoop->second; + tokenVector->push_back(tokens); + } +} + + +/********************************************************************** +* cSkindesignerOsdMenu +**********************************************************************/ +cSkindesignerOsdMenu::cSkindesignerOsdMenu(const char *Title, int c0, int c1, int c2, int c3, int c4) : cOsdMenu(Title, c0, c1, c2, c3, c4) { + init = true; + displayText = false; + sdDisplayMenu = NULL; + pluginName = ""; + SetMenuCategory(mcPlugin); + SetSkinDesignerDisplayMenu(); +} + +cSkindesignerOsdMenu::~cSkindesignerOsdMenu() { + +} + +void cSkindesignerOsdMenu::SetPluginMenu(int menu, eMenuType type) { + if (type == mtList) + displayText = false; + else if (type == mtText) + displayText = true; + + if (sdDisplayMenu) { + sdDisplayMenu->SetPluginMenu(pluginName, menu, type, init); + } + init = false; +} + +bool cSkindesignerOsdMenu::SetSkinDesignerDisplayMenu(void) { + static cPlugin *pSkinDesigner = cPluginManager::GetPlugin("skindesigner"); + if (!pSkinDesigner) { + return false; + } + GetDisplayMenu call; + bool ok = pSkinDesigner->Service("GetDisplayMenu", &call); + if (ok && call.displayMenu) { + sdDisplayMenu = call.displayMenu; + return true; + } + return false; +} + +void cSkindesignerOsdMenu::ClearTokens(void) { + text = ""; + stringTokens.clear(); + intTokens.clear(); + loopTokens.clear(); +} + +void cSkindesignerOsdMenu::AddStringToken(string key, string value) { + stringTokens.insert(pair(key, value)); +} + +void cSkindesignerOsdMenu::AddIntToken(string key, int value) { + intTokens.insert(pair(key, value)); +} + +void cSkindesignerOsdMenu::AddLoopToken(string loopName, map &tokens) { + map > >::iterator hitLoop = loopTokens.find(loopName); + if (hitLoop == loopTokens.end()) { + vector > tokenVector; + tokenVector.push_back(tokens); + loopTokens.insert(pair > >(loopName, tokenVector)); + } else { + vector > *tokenVector = &hitLoop->second; + tokenVector->push_back(tokens); + } +} + +void cSkindesignerOsdMenu::TextKeyLeft(void) { + if (!displayText) + return; + DisplayMenu()->Scroll(true, true); +} + +void cSkindesignerOsdMenu::TextKeyRight(void) { + if (!displayText) + return; + DisplayMenu()->Scroll(false, true); +} + +void cSkindesignerOsdMenu::TextKeyUp(void) { + if (!displayText) + return; + DisplayMenu()->Scroll(true, false); +} + +void cSkindesignerOsdMenu::TextKeyDown(void) { + if (!displayText) + return; + DisplayMenu()->Scroll(false, false); +} + +void cSkindesignerOsdMenu::Display(void) { + if (displayText) { + if (sdDisplayMenu) { + sdDisplayMenu->SetTitle(Title()); + if (sdDisplayMenu->SetPluginText(&stringTokens, &intTokens, &loopTokens)) { + sdDisplayMenu->Flush(); + } else { + DisplayMenu()->Clear(); + DisplayMenu()->SetTitle(Title()); + DisplayMenu()->SetText(text.c_str(), false); + DisplayMenu()->Flush(); + } + } else { + DisplayMenu()->Clear(); + DisplayMenu()->SetTitle(Title()); + DisplayMenu()->SetText(text.c_str(), false); + DisplayMenu()->Flush(); + } + return; + } + if (sdDisplayMenu) { + sdDisplayMenu->SetTitle(Title()); + for (cOsdItem *item = First(); item; item = Next(item)) { + cSkindesignerOsdItem *sdItem = dynamic_cast(item); + if (sdItem) { + sdItem->SetDisplayMenu(sdDisplayMenu); + } + } + } + cOsdMenu::Display(); +} diff --git a/libskindesigner/skindesignerosdbase.h b/libskindesigner/skindesignerosdbase.h new file mode 100644 index 0000000..88068af --- /dev/null +++ b/libskindesigner/skindesignerosdbase.h @@ -0,0 +1,62 @@ +#ifndef __SKINDESIGNEROSDBASE_H +#define __SKINDESIGNEROSDBASE_H + +#include +#include +#include +#include +#include +#include +#include +#include "services.h" + +class cSkindesignerOsdItem : public cOsdItem { +private: + cSDDisplayMenu *sdDisplayMenu; + map < string, string > stringTokens; + map < string, int > intTokens; + map < string, vector< map< string, string > > > loopTokens; +protected: +public: + cSkindesignerOsdItem(eOSState State = osUnknown); + cSkindesignerOsdItem(const char *Text, eOSState State = osUnknown, bool Selectable = true); + virtual ~cSkindesignerOsdItem(); + virtual void SetMenuItem(cSkinDisplayMenu *DisplayMenu, int Index, bool Current, bool Selectable); + void SetDisplayMenu(cSDDisplayMenu *sdDisplayMenu) { this->sdDisplayMenu = sdDisplayMenu; }; + void AddStringToken(string key, string value); + void AddIntToken(string key, int value); + void AddLoopToken(string loopName, map &tokens); +}; + + +class cSkindesignerOsdMenu : public cOsdMenu { +private: + bool init; + bool displayText; + string pluginName; + cSDDisplayMenu *sdDisplayMenu; + string text; + map < string, string > stringTokens; + map < string, int > intTokens; + map < string, vector< map< string, string > > > loopTokens; + bool SetSkinDesignerDisplayMenu(void); +protected: + void ClearTokens(void); + void SetPluginName(string name) {pluginName = name; }; + void SetPluginMenu(int menu, eMenuType type); + void SetText(string text) { this->text = text; }; + void AddStringToken(string key, string value); + void AddIntToken(string key, int value); + void AddLoopToken(string loopName, map &tokens); + void TextKeyLeft(void); + void TextKeyRight(void); + void TextKeyUp(void); + void TextKeyDown(void); +public: + cSkindesignerOsdMenu(const char *Title, int c0 = 0, int c1 = 0, int c2 = 0, int c3 = 0, int c4 = 0); + virtual ~cSkindesignerOsdMenu(); + virtual void Display(void); +}; + +#endif // __SKINDESIGNEROSDBASE_H + diff --git a/po/de_DE.po b/po/de_DE.po new file mode 100644 index 0000000..429fe53 --- /dev/null +++ b/po/de_DE.po @@ -0,0 +1,106 @@ +# Copyright (C) 2015 +# This file is distributed under the same license as the PACKAGE package. +# iFIRST AUTHOR , 2015. +# +msgid "" +msgstr "" +"Project-Id-Version: vdr-weatherforecast 0.0.1\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2015-01-14 13:26+0100\n" +"PO-Revision-Date: 2015-01-12 11:46+0100\n" +"Last-Translator: Louis Braun \n" +"Language-Team: \n" +"Language: de_DE \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=utf-8\n" +"Content-Transfer-Encoding: 8bit\n" + +msgid "City" +msgstr "Wohnort" + +msgid "Latitude" +msgstr "Latítude" + +msgid "Longitude" +msgstr "Longitude" + +msgid "Weather Forecast" +msgstr "" + +msgid "Current Weather" +msgstr "Aktuelles Wetter" + +msgid "Next 48 Hours" +msgstr "Die nächsten 48 Stunden" + +msgid "Next 7 Days" +msgstr "Nächste 7 Tage" + +msgid "Weather for" +msgstr "Wetter für" + +msgid "Current Temperature" +msgstr "Aktuelles Wetter" + +msgid "Apparent Temperature" +msgstr "Gefühlte Temperatur" + +msgid "Minimum Temperature today" +msgstr "Heutige minimale Temperatur" + +msgid "Maximum Temperature today" +msgstr "Heutige maximale Temperatur" + +msgid "Precipitation Probability" +msgstr "Niederschlagswahrscheinlichkeit" + +msgid "Precipitation Intensity" +msgstr "Regenintensität" + +msgid "Humidity" +msgstr "Luftfeuchtigkeit" + +msgid "Wind Speed" +msgstr "Windgeschwindigkeit" + +msgid "Wind Bearing" +msgstr "Windrichtung" + +msgid "Visibility" +msgstr "Sichtweite" + +msgid "Cloud Cover" +msgstr "Bewölkung" + +msgid "Pressure" +msgstr "Luftdruck" + +msgid "Ozone" +msgstr "Ozon" + +msgid "Weather in the next 48 Hours" +msgstr "Wetter in den nächsten 48 Stunden" + +msgid "Temperature" +msgstr "Temperatur" + +msgid "felt" +msgstr "gefühlt" + +msgid "Intensity" +msgstr "Stärke" + +msgid "Wind" +msgstr "Wind" + +msgid "Weather the next 7 days" +msgstr "Wetter in den nächsten 7 Tagen" + +msgid "Minimum Temperature" +msgstr "Minimale Temperatur" + +msgid "at" +msgstr "um" + +msgid "Maximum Temperature" +msgstr "Maximale Temperatur" diff --git a/services.h b/services.h new file mode 100644 index 0000000..fce080a --- /dev/null +++ b/services.h @@ -0,0 +1,49 @@ +#ifndef __WEATHERFORECASTSERVICES_H +#define __WEATHERFORECASTSERVICES_H + +#include + +class cServiceCurrentWeather { +public: + cServiceCurrentWeather(void) { + timeStamp = ""; + temperature = ""; + minTemperature = ""; + maxTemperature = ""; + apparentTemperature = ""; + summary = ""; + icon = ""; + precipitationIntensity = ""; + precipitationProbability = 0; + precipitationType = ""; + humidity = 0; + windSpeed = ""; + windBearing = 0; + windBearingString = ""; + visibility = ""; + cloudCover = 0; + pressure = ""; + ozone = ""; + }; + std::string timeStamp; + std::string temperature; + std::string apparentTemperature; + std::string minTemperature; + std::string maxTemperature; + std::string summary; + std::string icon; + std::string precipitationIntensity; + int precipitationProbability; + std::string precipitationType; + int humidity; + std::string windSpeed; + int windBearing; + std::string windBearingString; + std::string visibility; + int cloudCover; + std::string pressure; + std::string ozone; +}; + +#endif //__WEATHERFORECASTSERVICES_H + diff --git a/setup.c b/setup.c new file mode 100644 index 0000000..55ad504 --- /dev/null +++ b/setup.c @@ -0,0 +1,47 @@ +#include "setup.h" +#include "tools/jsonhelpers.h" + +cWeatherforecastSetup::cWeatherforecastSetup() { + data = weatherConfig; + strn0cpy(city, data.city.c_str(), sizeof(city)); + string strLat = FloatToString(data.lat); + string strLon = FloatToString(data.lon); + strn0cpy(lat, strLat.c_str(), sizeof(lat)); + strn0cpy(lon, strLon.c_str(), sizeof(lon)); + Setup(); +} + +cWeatherforecastSetup::~cWeatherforecastSetup() { +} + + +void cWeatherforecastSetup::Setup(void) { + int current = Current(); + Clear(); + + Add(new cMenuEditStrItem(tr("City"), city, sizeof(city), trVDR(FileNameChars))); + Add(new cMenuEditStrItem(tr("Latitude"), lat, sizeof(lat), trVDR(FileNameChars))); + Add(new cMenuEditStrItem(tr("Longitude"), lon, sizeof(lon), trVDR(FileNameChars))); + + SetCurrent(Get(current)); + Display(); +} + +eOSState cWeatherforecastSetup::ProcessKey(eKeys Key) { + eOSState state = cMenuSetupPage::ProcessKey(Key); + switch (state) { + default: break; + } + return state; +} + +void cWeatherforecastSetup::Store(void) { + weatherConfig = data; + SetupStore("city", city); + SetupStore("lat", lat); + SetupStore("lon", lon); + + weatherConfig.city = city; + weatherConfig.lat = atod(lat); + weatherConfig.lon = atod(lon); +} \ No newline at end of file diff --git a/setup.h b/setup.h new file mode 100644 index 0000000..6800bad --- /dev/null +++ b/setup.h @@ -0,0 +1,22 @@ +#ifndef __WEATHERFORECAST_SETUP_H +#define __WEATHERFORECAST_SETUP_H + +#include "config.h" + +extern cWeatherforecastConfig weatherConfig; +using namespace std; + +class cWeatherforecastSetup : public cMenuSetupPage { + public: + cWeatherforecastSetup(void); + virtual ~cWeatherforecastSetup(); + char city[100]; + char lat[10]; + char lon[10]; + private: + cWeatherforecastConfig data; + void Setup(void); + virtual eOSState ProcessKey(eKeys Key); + virtual void Store(void); +}; +#endif //__WEATHERFORECAST_SETUP_H diff --git a/templates/plug-weatherforecast-weatherforecast.xml b/templates/plug-weatherforecast-weatherforecast.xml new file mode 100644 index 0000000..e88bdf1 --- /dev/null +++ b/templates/plug-weatherforecast-weatherforecast.xml @@ -0,0 +1,90 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/templates/plug-weatherforecast-weatherforecastdetailcurrent.xml b/templates/plug-weatherforecast-weatherforecastdetailcurrent.xml new file mode 100644 index 0000000..d9233fd --- /dev/null +++ b/templates/plug-weatherforecast-weatherforecastdetailcurrent.xml @@ -0,0 +1,35 @@ + + + + + + + + + + + + diff --git a/templates/plug-weatherforecast-weatherforecastdetaildaily.xml b/templates/plug-weatherforecast-weatherforecastdetaildaily.xml new file mode 100644 index 0000000..bde97df --- /dev/null +++ b/templates/plug-weatherforecast-weatherforecastdetaildaily.xml @@ -0,0 +1,40 @@ + + + + + + + + + + + + + diff --git a/templates/plug-weatherforecast-weatherforecastdetailhourly.xml b/templates/plug-weatherforecast-weatherforecastdetailhourly.xml new file mode 100644 index 0000000..8d225c3 --- /dev/null +++ b/templates/plug-weatherforecast-weatherforecastdetailhourly.xml @@ -0,0 +1,37 @@ + + + + + + + + + + + + diff --git a/templates/weathericons/clear-day.png b/templates/weathericons/clear-day.png new file mode 100644 index 0000000..2b06a14 Binary files /dev/null and b/templates/weathericons/clear-day.png differ diff --git a/templates/weathericons/clear-night.png b/templates/weathericons/clear-night.png new file mode 100644 index 0000000..9bbe49f Binary files /dev/null and b/templates/weathericons/clear-night.png differ diff --git a/templates/weathericons/cloudy.png b/templates/weathericons/cloudy.png new file mode 100644 index 0000000..9870c7f Binary files /dev/null and b/templates/weathericons/cloudy.png differ diff --git a/templates/weathericons/fog.png b/templates/weathericons/fog.png new file mode 100644 index 0000000..9bc88d2 Binary files /dev/null and b/templates/weathericons/fog.png differ diff --git a/templates/weathericons/partly-cloudy-day.png b/templates/weathericons/partly-cloudy-day.png new file mode 100644 index 0000000..b6df179 Binary files /dev/null and b/templates/weathericons/partly-cloudy-day.png differ diff --git a/templates/weathericons/partly-cloudy-night.png b/templates/weathericons/partly-cloudy-night.png new file mode 100644 index 0000000..aa358fb Binary files /dev/null and b/templates/weathericons/partly-cloudy-night.png differ diff --git a/templates/weathericons/rain.png b/templates/weathericons/rain.png new file mode 100644 index 0000000..8275003 Binary files /dev/null and b/templates/weathericons/rain.png differ diff --git a/templates/weathericons/sleet.png b/templates/weathericons/sleet.png new file mode 100644 index 0000000..1d99ae0 Binary files /dev/null and b/templates/weathericons/sleet.png differ diff --git a/templates/weathericons/snow.png b/templates/weathericons/snow.png new file mode 100644 index 0000000..939e22f Binary files /dev/null and b/templates/weathericons/snow.png differ diff --git a/templates/weathericons/wind.png b/templates/weathericons/wind.png new file mode 100644 index 0000000..d92ec15 Binary files /dev/null and b/templates/weathericons/wind.png differ diff --git a/tools/curlfuncs.c b/tools/curlfuncs.c new file mode 100644 index 0000000..ed1ea01 --- /dev/null +++ b/tools/curlfuncs.c @@ -0,0 +1,236 @@ +/* +Copyright (c) 2002, Mayukh Bose +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + +* Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. + +* Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. + +* Neither the name of Mayukh Bose nor the names of other +contributors may be used to endorse or promote products derived from +this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +/* + Change History: + 11/23/2004 - Removed the #include line because I didn't + need it. Wonder why I had it there in the first place :). + 10/20/2004 - Publicly released this code. +*/ +#include +#include +#include +#include +#include +#include "curlfuncs.h" + +#ifndef TRUE +#define TRUE 1 +#endif + +using namespace std; + +// Local function prototypes +int CurlDoPost(const char *url, string *sOutput, const string &sReferer, + struct curl_httppost *formpost, struct curl_slist *headerlist); + +namespace curlfuncs { + string sBuf; + bool bInitialized = false; + CURL *curl = NULL; +} + +size_t collect_data(void *ptr, size_t size, size_t nmemb, void *stream) +{ + string sTmp; + register size_t actualsize = size * nmemb; + if ((FILE *)stream == NULL) { + sTmp.assign((char *)ptr, actualsize); + curlfuncs::sBuf += sTmp; + } + else { + fwrite(ptr, size, nmemb, (FILE *)stream); + } + return actualsize; +} + +inline void InitCurlLibraryIfNeeded() +{ + if (!curlfuncs::bInitialized) { + curl_global_init(CURL_GLOBAL_ALL); + curlfuncs::curl = curl_easy_init(); + if (!curlfuncs::curl) + throw string("Could not create new curl instance"); + curl_easy_setopt(curlfuncs::curl, CURLOPT_NOPROGRESS, 1); // Do not show progress + curl_easy_setopt(curlfuncs::curl, CURLOPT_WRITEFUNCTION, collect_data); + curl_easy_setopt(curlfuncs::curl, CURLOPT_WRITEDATA, 0); // Set option to write to string + curl_easy_setopt(curlfuncs::curl, CURLOPT_FOLLOWLOCATION, TRUE); + curl_easy_setopt(curlfuncs::curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; Mayukh's libcurl wrapper http://www.mayukhbose.com/)"); + curlfuncs::bInitialized = true; + } +} + +int CurlGetUrl(const char *url, string *sOutput, const string &sReferer) +{ + InitCurlLibraryIfNeeded(); + + curl_easy_setopt(curlfuncs::curl, CURLOPT_URL, url); // Set the URL to get + if (sReferer != "") + curl_easy_setopt(curlfuncs::curl, CURLOPT_REFERER, sReferer.c_str()); + curl_easy_setopt(curlfuncs::curl, CURLOPT_HTTPGET, TRUE); + curl_easy_setopt(curlfuncs::curl, CURLOPT_WRITEDATA, 0); // Set option to write to string + curlfuncs::sBuf = ""; + if (curl_easy_perform(curlfuncs::curl) == 0) + *sOutput = curlfuncs::sBuf; + else { + // We have an error here mate! + *sOutput = ""; + return 0; + } + + return 1; +} + +int CurlGetUrlFile(const char *url, const char *filename, const string &sReferer) +{ + int nRet = 0; + InitCurlLibraryIfNeeded(); + + // Point the output to a file + FILE *fp; + if ((fp = fopen(filename, "w")) == NULL) + return 0; + + curl_easy_setopt(curlfuncs::curl, CURLOPT_WRITEDATA, fp); // Set option to write to file + curl_easy_setopt(curlfuncs::curl, CURLOPT_URL, url); // Set the URL to get + if (sReferer != "") + curl_easy_setopt(curlfuncs::curl, CURLOPT_REFERER, sReferer.c_str()); + curl_easy_setopt(curlfuncs::curl, CURLOPT_HTTPGET, TRUE); + if (curl_easy_perform(curlfuncs::curl) == 0) + nRet = 1; + else + nRet = 0; + + curl_easy_setopt(curlfuncs::curl, CURLOPT_WRITEDATA, NULL); // Set option back to default (string) + fclose(fp); + return nRet; +} + +int CurlPostUrl(const char *url, const string &sPost, string *sOutput, const string &sReferer) +{ + InitCurlLibraryIfNeeded(); + + int retval = 1; + string::size_type nStart = 0, nEnd, nPos; + string sTmp, sName, sValue; + struct curl_httppost *formpost=NULL; + struct curl_httppost *lastptr=NULL; + struct curl_slist *headerlist=NULL; + + // Add the POST variables here + while ((nEnd = sPost.find("##", nStart)) != string::npos) { + sTmp = sPost.substr(nStart, nEnd - nStart); + if ((nPos = sTmp.find("=")) == string::npos) + return 0; + sName = sTmp.substr(0, nPos); + sValue = sTmp.substr(nPos+1); + curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, sName.c_str(), CURLFORM_COPYCONTENTS, sValue.c_str(), CURLFORM_END); + nStart = nEnd + 2; + } + sTmp = sPost.substr(nStart); + if ((nPos = sTmp.find("=")) == string::npos) + return 0; + sName = sTmp.substr(0, nPos); + sValue = sTmp.substr(nPos+1); + curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, sName.c_str(), CURLFORM_COPYCONTENTS, sValue.c_str(), CURLFORM_END); + + retval = CurlDoPost(url, sOutput, sReferer, formpost, headerlist); + + curl_formfree(formpost); + curl_slist_free_all(headerlist); + return retval; +} + +int CurlPostRaw(const char *url, const string &sPost, string *sOutput, const string &sReferer) +{ + InitCurlLibraryIfNeeded(); + + int retval; + struct curl_httppost *formpost=NULL; + struct curl_slist *headerlist=NULL; + + curl_easy_setopt(curlfuncs::curl, CURLOPT_POSTFIELDS, sPost.c_str()); + curl_easy_setopt(curlfuncs::curl, CURLOPT_POSTFIELDSIZE, 0); //FIXME: Should this be the size instead, in case this is binary string? + + retval = CurlDoPost(url, sOutput, sReferer, formpost, headerlist); + + curl_formfree(formpost); + curl_slist_free_all(headerlist); + return retval; +} + +int CurlDoPost(const char *url, string *sOutput, const string &sReferer, + struct curl_httppost *formpost, struct curl_slist *headerlist) +{ + headerlist = curl_slist_append(headerlist, "Expect:"); + + // Now do the form post + curl_easy_setopt(curlfuncs::curl, CURLOPT_URL, url); + if (sReferer != "") + curl_easy_setopt(curlfuncs::curl, CURLOPT_REFERER, sReferer.c_str()); + curl_easy_setopt(curlfuncs::curl, CURLOPT_HTTPPOST, formpost); + + curl_easy_setopt(curlfuncs::curl, CURLOPT_WRITEDATA, 0); // Set option to write to string + curlfuncs::sBuf = ""; + if (curl_easy_perform(curlfuncs::curl) == 0) { + *sOutput = curlfuncs::sBuf; + return 1; + } + else { + // We have an error here mate! + *sOutput = ""; + return 0; + } +} + +void FreeCurlLibrary(void) +{ + if (curlfuncs::curl) + curl_easy_cleanup(curlfuncs::curl); + curl_global_cleanup(); + curlfuncs::bInitialized = false; +} + +int CurlSetCookieFile(char *filename) +{ + InitCurlLibraryIfNeeded(); + if (curl_easy_setopt(curlfuncs::curl, CURLOPT_COOKIEFILE, filename) != 0) + return 0; + if (curl_easy_setopt(curlfuncs::curl, CURLOPT_COOKIEJAR, filename) != 0) + return 0; + return 1; +} + +char *CurlEscape(const char *url) { + InitCurlLibraryIfNeeded(); + return curl_escape(url , strlen(url)); +} diff --git a/tools/curlfuncs.h b/tools/curlfuncs.h new file mode 100644 index 0000000..e0f76cf --- /dev/null +++ b/tools/curlfuncs.h @@ -0,0 +1,45 @@ +/* +Copyright (c) 2002, Mayukh Bose +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + +* Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. + +* Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. + +* Neither the name of Mayukh Bose nor the names of other +contributors may be used to endorse or promote products derived from +this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#ifndef __CURLFUNCS_H_20020513__ +#define __CURLFUNCS_H_20020513__ +#include + using namespace std; + +int CurlGetUrl(const char *url, string *sOutput, const string &sReferer=""); +int CurlGetUrlFile(const char *url, const char *filename, const string &sReferer=""); +void FreeCurlLibrary(void); +int CurlSetCookieFile(char *filename); +int CurlPostUrl(const char *url, const string &sPost, string *sOutput, const string &sReferer = ""); +int CurlPostRaw(const char *url, const string &sPost, string *sOutput, const string &sReferer = ""); +char *CurlEscape(const char *url); +#endif diff --git a/tools/filesystem.c b/tools/filesystem.c new file mode 100644 index 0000000..0f04100 --- /dev/null +++ b/tools/filesystem.c @@ -0,0 +1,53 @@ +#include +#include +#include +#include +#include +#include +#include "filesystem.h" + +bool FileExists(const string &fullpath) { + struct stat buffer; + return (stat (fullpath.c_str(), &buffer) == 0); +} + +bool FileExists(const string &path, const string &name, const string &ext) { + stringstream fileName; + fileName << path << name << "." << ext; + struct stat buffer; + return (stat (fileName.str().c_str(), &buffer) == 0); +} + +string ReadFromFile(const string &path) { + string line; + stringstream content; + ifstream myfile (path.c_str()); + if (myfile.is_open()) { + while ( getline (myfile,line) ) { + content << line << '\n'; + } + myfile.close(); + } + return content.str(); +} + +bool WriteIntoFile(const string &path, const string &content) { + if (content.size() < 5) + return false; + ofstream myfile; + myfile.open(path.c_str()); + if (myfile.is_open()) { + myfile << content; + myfile.close(); + return true; + } + return false; +} + +time_t FileCreationTime(const string &path) { + struct stat t_stat; + stat(path.c_str(), &t_stat); + struct tm * timeinfo = localtime(&t_stat.st_ctime); + time_t creationTime = mktime(timeinfo); + return creationTime; +} diff --git a/tools/filesystem.h b/tools/filesystem.h new file mode 100644 index 0000000..7ea0954 --- /dev/null +++ b/tools/filesystem.h @@ -0,0 +1,14 @@ +#ifndef __FILESYSTEM_H +#define __FILESYSTEM_H + +#include + +using namespace std; + +bool FileExists(const string &fullpath); +bool FileExists(const string &path, const string &name, const string &ext); +string ReadFromFile(const string &path); +bool WriteIntoFile(const string &path, const string &content); +time_t FileCreationTime(const string &path); + +#endif // __FILESYSTEM_H diff --git a/tools/jsonhelpers.c b/tools/jsonhelpers.c new file mode 100644 index 0000000..56cd854 --- /dev/null +++ b/tools/jsonhelpers.c @@ -0,0 +1,40 @@ +#include "jsonhelpers.h" + +int JsonGetInteger(json_t *jNode, string name) { + json_t *jInt = json_object_get(jNode, name.c_str()); + if (json_is_integer(jInt)) { + return json_integer_value(jInt); + } + return 0; +} + +float JsonGetFloat(json_t *jNode, string name) { + json_t *jFloat = json_object_get(jNode, name.c_str()); + if (json_is_real(jFloat)) { + return json_real_value(jFloat); + } + if (json_is_integer(jFloat)) { + return json_integer_value(jFloat); + } + return 0.0; +} + +string JsonGetString(json_t *jNode, string name) { + json_t *jString = json_object_get(jNode, name.c_str()); + if (json_is_string(jString)) { + return json_string_value(jString); + } + return ""; +} + +string FloatToString(float f) { + stringstream sTemp; + sTemp << f; + return sTemp.str(); +} + +string IntToString(int i) { + stringstream sTemp; + sTemp << i; + return sTemp.str(); +} diff --git a/tools/jsonhelpers.h b/tools/jsonhelpers.h new file mode 100644 index 0000000..852d94d --- /dev/null +++ b/tools/jsonhelpers.h @@ -0,0 +1,17 @@ +#ifndef __JSONHELPERS_H +#define __JSONHELPERS_H + +#include +#include +#include + +using namespace std; + +int JsonGetInteger(json_t *jNode, string name); +float JsonGetFloat(json_t *jNode, string name); +string JsonGetString(json_t *jNode, string name); + +string FloatToString(float f); +string IntToString(int i); + +#endif //__JSONHELPERS_H \ No newline at end of file diff --git a/weatherforecast.c b/weatherforecast.c new file mode 100644 index 0000000..49c01ee --- /dev/null +++ b/weatherforecast.c @@ -0,0 +1,161 @@ +/* + * weatherforecast.c: A plugin for the Video Disk Recorder + * + * See the README file for copyright information and how to reach the author. + * + * $Id$ + */ + +#include +#include "config.h" +#include "setup.h" +#include "libforecastio/forecastio.h" +#include "libforecastio/locator.h" +#include "libskindesigner/services.h" +#include "weatherosd.h" +#include "services.h" + +//*************************************************************************** +// Constants +//*************************************************************************** +static const char *VERSION = "0.0.1"; +static const char *DESCRIPTION = "Weatherforecast based on forecast.io"; +static const char *MAINMENUENTRY = "WeatherForecast"; + +//*************************************************************************** +// Globals +//*************************************************************************** +cWeatherforecastConfig weatherConfig; + +//*************************************************************************** +// cPluginWeatherforecast +//*************************************************************************** +class cPluginWeatherforecast : public cPlugin { +private: + cForecastIO *forecastIO; +public: + cPluginWeatherforecast(void); + virtual ~cPluginWeatherforecast(); + virtual const char *Version(void) { return VERSION; } + virtual const char *Description(void) { return DESCRIPTION; } + virtual const char *CommandLineHelp(void); + virtual bool ProcessArgs(int argc, char *argv[]); + virtual bool Initialize(void); + virtual bool Start(void); + virtual void Stop(void); + virtual void Housekeeping(void); + virtual void MainThreadHook(void); + virtual cString Active(void); + virtual time_t WakeupTime(void); + virtual const char *MainMenuEntry(void) { return MAINMENUENTRY; } + virtual cOsdObject *MainMenuAction(void); + virtual cMenuSetupPage *SetupMenu(void); + virtual bool SetupParse(const char *Name, const char *Value); + virtual bool Service(const char *Id, void *Data = NULL); + virtual const char **SVDRPHelpPages(void); + virtual cString SVDRPCommand(const char *Command, const char *Option, int &ReplyCode); +}; + +cPluginWeatherforecast::cPluginWeatherforecast(void) { + forecastIO = NULL; +} + +cPluginWeatherforecast::~cPluginWeatherforecast() { + if (forecastIO) + delete forecastIO; +} + +const char *cPluginWeatherforecast::CommandLineHelp(void) { + return NULL; +} + +bool cPluginWeatherforecast::ProcessArgs(int argc, char *argv[]) { + return true; +} + +bool cPluginWeatherforecast::Initialize(void) { + //check geolocation on first start + if (weatherConfig.city.size() == 0) { + dsyslog("weatherforecast: location not set, setting from own public IP"); + cForecastLocator locator; + if (locator.ReadLocationByIP()) { + locator.WriteToSetup(this); + } + dsyslog("weatherforecast: location wrote to setup"); + } + return true; +} + +bool cPluginWeatherforecast::Start(void) { + string cacheDir = cPlugin::CacheDirectory(PLUGIN_NAME_I18N); + forecastIO = new cForecastIO(cacheDir); + forecastIO->Start(); + + RegisterPlugin reg; + reg.name = "weatherforecast"; + reg.SetMenu(meRoot, "weatherforecast.xml"); + reg.SetMenu(meDetailCurrent, "weatherforecastdetailcurrent.xml"); + reg.SetMenu(meDetailHourly, "weatherforecastdetailhourly.xml"); + reg.SetMenu(meDetailDaily, "weatherforecastdetaildaily.xml"); + static cPlugin *pSkinDesigner = cPluginManager::GetPlugin("skindesigner"); + if (pSkinDesigner) { + pSkinDesigner->Service("RegisterPlugin", ®); + } else { + esyslog("weatherforecast: skindesigner not available"); + } + + return true; +} + +void cPluginWeatherforecast::Stop(void) { + +} + +void cPluginWeatherforecast::Housekeeping(void) { + +} + +void cPluginWeatherforecast::MainThreadHook(void) { + +} + +cString cPluginWeatherforecast::Active(void) { + return NULL; +} + +time_t cPluginWeatherforecast::WakeupTime(void) { + return 0; +} + +cOsdObject *cPluginWeatherforecast::MainMenuAction(void) { + cOsdObject *menu = new cWeatherOsd(forecastIO); + return menu; +} + +cMenuSetupPage *cPluginWeatherforecast::SetupMenu(void) { + return new cWeatherforecastSetup(); +} + +bool cPluginWeatherforecast::SetupParse(const char *Name, const char *Value) { + return weatherConfig.SetupParse(Name, Value); +} + +bool cPluginWeatherforecast::Service(const char *Id, void *Data) { + if (Data == NULL) + return false; + if (strcmp(Id, "GetCurrentWeather") == 0) { + cServiceCurrentWeather* call = (cServiceCurrentWeather*) Data; + return forecastIO->SetCurrentWeather(call); + } + return false; +} + +const char **cPluginWeatherforecast::SVDRPHelpPages(void) { + return NULL; +} + +cString cPluginWeatherforecast::SVDRPCommand(const char *Command, const char *Option, int &ReplyCode) { + return NULL; +} + +VDRPLUGINCREATOR(cPluginWeatherforecast); // Don't touch this! diff --git a/weatherosd.c b/weatherosd.c new file mode 100644 index 0000000..af1ba8f --- /dev/null +++ b/weatherosd.c @@ -0,0 +1,371 @@ +#include "config.h" +#include "tools/jsonhelpers.h" +#include "weatherosd.h" + +extern cWeatherforecastConfig weatherConfig; + +cWeatherOsd::cWeatherOsd(cForecastIO *forecastIO) : cSkindesignerOsdMenu("Weather Forecast") { + isDetailedView = false; + lastRootMenuElement = 0; + SetPluginName("weatherforecast"); + this->forecastIO = forecastIO; + SetRootMenu(); +} + +cWeatherOsd::~cWeatherOsd(void) { + +} + +eOSState cWeatherOsd::ProcessKey(eKeys key) { + eOSState state = cOsdMenu::ProcessKey(key); + switch (key) { + case kOk: { + if (isDetailedView) { + SetRootMenu(); + } else { + int element = Current(); + lastRootMenuElement = element; + if (element == 0) + SetDetailViewCurrent(); + else if (element == 1) + SetDetailViewHourly(); + else if (element == 2) + SetDetailViewDaily(); + } + state = osContinue; + break; + } case kLeft: { + TextKeyLeft(); + state = osContinue; + break; + } case kRight: { + TextKeyRight(); + state = osContinue; + break; + } case kUp: { + TextKeyUp(); + state = osContinue; + break; + } case kDown: { + TextKeyDown(); + state = osContinue; + break; + } case kBack: { + if (isDetailedView) { + SetRootMenu(); + state = osContinue; + } + break; + } default: + break; + } + return state; +} + +void cWeatherOsd::SetRootMenu(void) { + isDetailedView = false; + SetPluginMenu(meRoot, mtList); + Clear(); + SetTitle(tr("Weather Forecast")); + + cSkindesignerOsdItem *currentWeather = new cSkindesignerOsdItem(); + string itemLabelCurrent = tr("Current Weather"); + currentWeather->SetText(itemLabelCurrent.c_str()); + currentWeather->AddStringToken("menuitemtext", itemLabelCurrent.c_str()); + currentWeather->AddIntToken("iscurrent", 1); + currentWeather->AddIntToken("ishourly", 0); + currentWeather->AddIntToken("isdaily", 0); + currentWeather->AddStringToken("city", weatherConfig.city); + currentWeather->AddStringToken("latitude", FloatToString(weatherConfig.lat)); + currentWeather->AddStringToken("longitude", FloatToString(weatherConfig.lon)); + + cForecast *currentForecast = forecastIO->GetCurrentForecast(); + if (currentForecast) { + currentWeather->AddStringToken("timestamp", currentForecast->GetDateTimeString()); + currentWeather->AddStringToken("temperature", currentForecast->GetTemperatureString()); + currentWeather->AddStringToken("apparenttemperature", currentForecast->GetApparentTemperatureString()); + currentWeather->AddStringToken("summary", currentForecast->GetSummary()); + currentWeather->AddStringToken("icon", currentForecast->GetIcon()); + currentWeather->AddStringToken("precipitationintensity", currentForecast->GetPrecipIntensityString()); + currentWeather->AddIntToken("precipitationprobability", currentForecast->GetPrecipProbabilityPercent()); + currentWeather->AddStringToken("precipitationtype", currentForecast->GetPercipType()); + currentWeather->AddIntToken("humidity", currentForecast->GetHumidityPercent()); + currentWeather->AddStringToken("windspeed", currentForecast->GetWindSpeedString()); + currentWeather->AddIntToken("windbearing", currentForecast->GetWindBearing()); + currentWeather->AddStringToken("windbearingstring", currentForecast->GetWindBearingString()); + currentWeather->AddStringToken("visibility", currentForecast->GetVisibilityString()); + currentWeather->AddIntToken("cloudcover", currentForecast->GetCloudCoverPercent()); + currentWeather->AddStringToken("pressure", currentForecast->GetPressureString()); + currentWeather->AddStringToken("ozone", currentForecast->GetOzoneString()); + } + Add(currentWeather, (lastRootMenuElement == 0)?true:false); + + cSkindesignerOsdItem *nextHours = new cSkindesignerOsdItem(); + string itemLabelNextHours = tr("Next 48 Hours"); + nextHours->SetText(itemLabelNextHours.c_str()); + nextHours->AddStringToken("menuitemtext", itemLabelNextHours.c_str()); + nextHours->AddIntToken("iscurrent", 0); + nextHours->AddIntToken("ishourly", 1); + nextHours->AddIntToken("isdaily", 0); + nextHours->AddStringToken("city", weatherConfig.city); + nextHours->AddStringToken("latitude", FloatToString(weatherConfig.lat)); + nextHours->AddStringToken("longitude", FloatToString(weatherConfig.lon)); + + cForecasts *hourlyForecast = forecastIO->GetHourlyForecast(); + if (hourlyForecast) { + nextHours->AddStringToken("summary", hourlyForecast->GetSummary()); + nextHours->AddStringToken("icon", hourlyForecast->GetIcon()); + + cForecast *fc = hourlyForecast->GetFirstHourly(); + int num = 1; + while (fc) { + map tokens; + tokens.insert(pair("hourly[num]", IntToString(num++))); + tokens.insert(pair("hourly[timestamp]", fc->GetTimeString())); + tokens.insert(pair("hourly[temperature]", fc->GetTemperatureString())); + tokens.insert(pair("hourly[apparenttemperature]", fc->GetApparentTemperatureString())); + tokens.insert(pair("hourly[summary]", fc->GetSummary())); + tokens.insert(pair("hourly[icon]", fc->GetIcon())); + tokens.insert(pair("hourly[precipitationintensity]", fc->GetPrecipIntensityString())); + tokens.insert(pair("hourly[precipitationprobability]", IntToString(fc->GetPrecipProbabilityPercent()))); + tokens.insert(pair("hourly[precipitationtype]", fc->GetPercipType())); + tokens.insert(pair("hourly[humidity]", IntToString(fc->GetHumidityPercent()))); + tokens.insert(pair("hourly[windspeed]", fc->GetWindSpeedString())); + tokens.insert(pair("hourly[windbearing]", IntToString(fc->GetWindBearing()))); + tokens.insert(pair("hourly[windbearingstring]", fc->GetWindBearingString())); + tokens.insert(pair("hourly[visibility]", fc->GetVisibilityString())); + tokens.insert(pair("hourly[cloudcover]", IntToString(fc->GetCloudCoverPercent()))); + tokens.insert(pair("hourly[pressure]", fc->GetPressureString())); + tokens.insert(pair("hourly[ozone]", fc->GetOzoneString())); + nextHours->AddLoopToken("hourly", tokens); + fc = hourlyForecast->GetNext(); + } + } + + Add(nextHours, (lastRootMenuElement == 1)?true:false); + + cSkindesignerOsdItem *nextDays = new cSkindesignerOsdItem(); + string itemLabelNextDays = tr("Next 7 Days"); + nextDays->SetText(itemLabelNextDays.c_str()); + nextDays->AddStringToken("menuitemtext", itemLabelNextDays.c_str()); + nextDays->AddIntToken("iscurrent", 0); + nextDays->AddIntToken("ishourly", 0); + nextDays->AddIntToken("isdaily", 1); + nextDays->AddStringToken("city", weatherConfig.city); + nextDays->AddStringToken("latitude", FloatToString(weatherConfig.lat)); + nextDays->AddStringToken("longitude", FloatToString(weatherConfig.lon)); + + cForecasts *dailyForecast = forecastIO->GetDailyForecast(); + if (dailyForecast) { + nextDays->AddStringToken("summary", dailyForecast->GetSummary()); + nextDays->AddStringToken("icon", dailyForecast->GetIcon()); + + cForecast *fc = dailyForecast->GetFirstDaily(); + while (fc) { + map tokens; + tokens.insert(pair("daily[day]", fc->GetDateString())); + tokens.insert(pair("daily[dayname]", fc->GetDayName())); + tokens.insert(pair("daily[temperaturemin]", fc->GetTemperatureMinString())); + tokens.insert(pair("daily[temperaturemintime]", fc->GetTemperatureMinTimeString())); + tokens.insert(pair("daily[temperaturemax]", fc->GetTemperatureMaxString())); + tokens.insert(pair("daily[temperaturemaxtime]", fc->GetTemperatureMaxTimeString())); + tokens.insert(pair("daily[summary]", fc->GetSummary())); + tokens.insert(pair("daily[icon]", fc->GetIcon())); + tokens.insert(pair("daily[precipitationintensity]", fc->GetPrecipIntensityString())); + tokens.insert(pair("daily[precipitationprobability]", IntToString(fc->GetPrecipProbabilityPercent()))); + tokens.insert(pair("daily[precipitationtype]", fc->GetPercipType())); + tokens.insert(pair("daily[humidity]", IntToString(fc->GetHumidityPercent()))); + tokens.insert(pair("daily[windspeed]", fc->GetWindSpeedString())); + tokens.insert(pair("daily[windbearing]", IntToString(fc->GetWindBearing()))); + tokens.insert(pair("daily[windbearingstring]", fc->GetWindBearingString())); + tokens.insert(pair("daily[visibility]", fc->GetVisibilityString())); + tokens.insert(pair("daily[cloudcover]", IntToString(fc->GetCloudCoverPercent()))); + tokens.insert(pair("daily[pressure]", fc->GetPressureString())); + tokens.insert(pair("daily[ozone]", fc->GetOzoneString())); + nextDays->AddLoopToken("daily", tokens); + fc = dailyForecast->GetNext(); + } + } + + Add(nextDays, (lastRootMenuElement == 2)?true:false); + + Display(); +} + +void cWeatherOsd::SetDetailViewCurrent(void) { + isDetailedView = true; + SetPluginMenu(meDetailCurrent, mtText); + ClearTokens(); + Clear(); + SetTitle(tr("Current Weather")); + cForecast *current = forecastIO->GetCurrentForecast(); + if (!current) + return; + cForecasts *daily = forecastIO->GetDailyForecast(); + if (!daily) + return; + cForecast *today = daily->GetFirstDaily(); + if (!today) + return; + + stringstream plainText; + plainText << tr("Weather for") << " " << weatherConfig.city << " " << current->GetDateTimeString() << ": " << current->GetSummary() << "\n"; + plainText << tr("Current Temperature") << ": " << current->GetTemperatureString() << "°C" << "\n"; + plainText << tr("Apparent Temperature") << ": " << current->GetApparentTemperatureString() << "°C" << "\n"; + plainText << tr("Minimum Temperature today") << ": " << today->GetTemperatureMinString() << "°C" << "\n"; + plainText << tr("Maximum Temperature today") << ": " << today->GetTemperatureMaxString() << "°C" << "\n"; + plainText << tr("Precipitation Probability") << ": " << current->GetPrecipProbabilityPercent() << " %" << "\n"; + plainText << tr("Precipitation Intensity") << ": " << current->GetPrecipIntensityString() << " l/qm" << "\n"; + plainText << tr("Humidity") << ": " << current->GetHumidityPercent() << " %" << "\n"; + plainText << tr("Wind Speed") << ": " << current->GetWindSpeedString() << " km/h" << "\n"; + plainText << tr("Wind Bearing") << ": " << current->GetWindBearing() << "° " << current->GetWindBearingString() << "\n"; + plainText << tr("Visibility") << ": " << current->GetVisibilityString() << " km" << "\n"; + plainText << tr("Cloud Cover") << ": " << current->GetCloudCoverPercent() << " %" << "\n"; + plainText << tr("Pressure") << ": " << current->GetPressureString() << " HPa" << "\n"; + plainText << tr("Ozone") << ": " << current->GetOzoneString() << " DU" << "\n"; + SetText(plainText.str().c_str()); + + + AddStringToken("menuheader", tr("Current Weather")); + AddStringToken("city", weatherConfig.city); + AddStringToken("latitude", FloatToString(weatherConfig.lat)); + AddStringToken("longitude", FloatToString(weatherConfig.lon)); + AddStringToken("timestamp", current->GetDateTimeString()); + AddStringToken("temperature", current->GetTemperatureString()); + AddStringToken("apparenttemperature", current->GetApparentTemperatureString()); + AddStringToken("mintemperature", today->GetTemperatureMinString()); + AddStringToken("maxtemperature", today->GetTemperatureMaxString()); + AddStringToken("summary", current->GetSummary()); + AddStringToken("icon", current->GetIcon()); + AddStringToken("precipitationintensity", current->GetPrecipIntensityString()); + AddIntToken("precipitationprobability", current->GetPrecipProbabilityPercent()); + AddStringToken("precipitationtype", current->GetPercipType()); + AddIntToken("humidity", current->GetHumidityPercent()); + AddStringToken("windspeed", current->GetWindSpeedString()); + AddIntToken("windbearing", current->GetWindBearing()); + AddStringToken("windbearingstring", current->GetWindBearingString()); + AddStringToken("visibility", current->GetVisibilityString()); + AddIntToken("cloudcover", current->GetCloudCoverPercent()); + AddStringToken("pressure", current->GetPressureString()); + AddStringToken("ozone", current->GetOzoneString()); + + Display(); +} + +void cWeatherOsd::SetDetailViewHourly(void) { + isDetailedView = true; + SetPluginMenu(meDetailHourly, mtText); + Clear(); + ClearTokens(); + SetTitle(tr("Weather in the next 48 Hours")); + + cForecasts *hourly = forecastIO->GetHourlyForecast(); + if (!hourly) + return; + + stringstream plainText; + cForecast *fc = hourly->GetFirstHourly(); + + AddStringToken("menuheader", tr("Weather in the next 48 Hours")); + AddStringToken("summary", hourly->GetSummary()); + AddStringToken("icon", hourly->GetIcon()); + AddStringToken("city", weatherConfig.city); + AddStringToken("latitude", FloatToString(weatherConfig.lat)); + AddStringToken("longitude", FloatToString(weatherConfig.lon)); + + int num = 1; + while (fc) { + map tokens; + tokens.insert(pair("hourly[num]", IntToString(num++))); + tokens.insert(pair("hourly[timestamp]", fc->GetTimeString())); + tokens.insert(pair("hourly[temperature]", fc->GetTemperatureString())); + tokens.insert(pair("hourly[apparenttemperature]", fc->GetApparentTemperatureString())); + tokens.insert(pair("hourly[summary]", fc->GetSummary())); + tokens.insert(pair("hourly[icon]", fc->GetIcon())); + tokens.insert(pair("hourly[precipitationintensity]", fc->GetPrecipIntensityString())); + tokens.insert(pair("hourly[precipitationprobability]", IntToString(fc->GetPrecipProbabilityPercent()))); + tokens.insert(pair("hourly[precipitationtype]", fc->GetPercipType())); + tokens.insert(pair("hourly[humidity]", IntToString(fc->GetHumidityPercent()))); + tokens.insert(pair("hourly[windspeed]", fc->GetWindSpeedString())); + tokens.insert(pair("hourly[windbearing]", IntToString(fc->GetWindBearing()))); + tokens.insert(pair("hourly[windbearingstring]", fc->GetWindBearingString())); + tokens.insert(pair("hourly[visibility]", fc->GetVisibilityString())); + tokens.insert(pair("hourly[cloudcover]", IntToString(fc->GetCloudCoverPercent()))); + tokens.insert(pair("hourly[pressure]", fc->GetPressureString())); + tokens.insert(pair("hourly[ozone]", fc->GetOzoneString())); + AddLoopToken("hourly", tokens); + + plainText << fc->GetDateTimeString() << ": " << fc->GetSummary() << "\n"; + plainText << tr("Temperature") << ": " << fc->GetTemperatureString() << "°C, " << tr("felt") << " " << fc->GetApparentTemperatureString() << "°C""\n"; + plainText << tr("Precipitation Probability") << ": " << fc->GetPrecipProbabilityPercent() << " %, " << tr("Intensity") << ": " << fc->GetPrecipIntensityString() << " l/qm""\n"; + plainText << tr("Wind") << ": " << fc->GetWindSpeedString() << " km/h, " << fc->GetWindBearing() << "° " << fc->GetWindBearingString() << "\n"; + plainText << tr("Cloud Cover") << ": " << fc->GetCloudCoverPercent() << " %" << "\n"; + plainText << "\n"; + + fc = hourly->GetNext(); + } + + SetText(plainText.str().c_str()); + Display(); +} + +void cWeatherOsd::SetDetailViewDaily(void) { + isDetailedView = true; + SetPluginMenu(meDetailDaily, mtText); + Clear(); + ClearTokens(); + SetTitle(tr("Weather the next 7 days")); + + cForecasts *daily = forecastIO->GetDailyForecast(); + if (!daily) + return; + + stringstream plainText; + cForecast *fc = daily->GetFirstDaily(); + + AddStringToken("menuheader", tr("Weather the next 7 days")); + AddStringToken("summary", daily->GetSummary()); + AddStringToken("icon", daily->GetIcon()); + AddStringToken("city", weatherConfig.city); + AddStringToken("latitude", FloatToString(weatherConfig.lat)); + AddStringToken("longitude", FloatToString(weatherConfig.lon)); + + while (fc) { + map tokens; + tokens.insert(pair("daily[day]", fc->GetDateString())); + tokens.insert(pair("daily[dayname]", fc->GetDayName())); + tokens.insert(pair("daily[temperaturemin]", fc->GetTemperatureMinString())); + tokens.insert(pair("daily[temperaturemintime]", fc->GetTemperatureMinTimeString())); + tokens.insert(pair("daily[temperaturemax]", fc->GetTemperatureMaxString())); + tokens.insert(pair("daily[temperaturemaxtime]", fc->GetTemperatureMaxTimeString())); + tokens.insert(pair("daily[summary]", fc->GetSummary())); + tokens.insert(pair("daily[icon]", fc->GetIcon())); + tokens.insert(pair("daily[precipitationintensity]", fc->GetPrecipIntensityString())); + tokens.insert(pair("daily[precipitationprobability]", IntToString(fc->GetPrecipProbabilityPercent()))); + tokens.insert(pair("daily[precipitationtype]", fc->GetPercipType())); + tokens.insert(pair("daily[humidity]", IntToString(fc->GetHumidityPercent()))); + tokens.insert(pair("daily[windspeed]", fc->GetWindSpeedString())); + tokens.insert(pair("daily[windbearing]", IntToString(fc->GetWindBearing()))); + tokens.insert(pair("daily[windbearingstring]", fc->GetWindBearingString())); + tokens.insert(pair("daily[visibility]", fc->GetVisibilityString())); + tokens.insert(pair("daily[cloudcover]", IntToString(fc->GetCloudCoverPercent()))); + tokens.insert(pair("daily[pressure]", fc->GetPressureString())); + tokens.insert(pair("daily[ozone]", fc->GetOzoneString())); + AddLoopToken("daily", tokens); + + plainText << fc->GetDayName() << " " << fc->GetDateString() << ": " << fc->GetSummary() << "\n"; + plainText << tr("Minimum Temperature") << ": " << fc->GetTemperatureMinString() << "°C " << tr("at") << " " << fc->GetTemperatureMinTimeString() << " ,"; + plainText << tr("Maximum Temperature") << ": " << fc->GetTemperatureMaxString() << "°C " << tr("at") << " " << fc->GetTemperatureMaxTimeString() << "\n"; + plainText << tr("Precipitation Probability") << ": " << fc->GetPrecipProbabilityPercent() << " %, " << tr("Intensity") << ": " << fc->GetPrecipIntensityString() << " l/qm""\n"; + plainText << tr("Wind") << ": " << fc->GetWindSpeedString() << " km/h, " << fc->GetWindBearing() << "° " << fc->GetWindBearingString() << "\n"; + plainText << tr("Cloud Cover") << ": " << fc->GetCloudCoverPercent() << " %" << "\n"; + plainText << tr("Pressure") << ": " << fc->GetPressureString() << " HPa" << "\n"; + plainText << tr("Ozone") << ": " << fc->GetOzoneString() << " DU" << "\n"; + plainText << "\n"; + + fc = daily->GetNext(); + } + + SetText(plainText.str().c_str()); + Display(); +} diff --git a/weatherosd.h b/weatherosd.h new file mode 100644 index 0000000..d1e757f --- /dev/null +++ b/weatherosd.h @@ -0,0 +1,31 @@ +#ifndef __WEATHEROSD_H +#define __WEATHEROSD_H + +#include +#include +#include "libskindesigner/skindesignerosdbase.h" +#include "libforecastio/forecastio.h" + +enum eMenus { + meRoot, + meDetailCurrent, + meDetailHourly, + meDetailDaily +}; + +class cWeatherOsd : public cSkindesignerOsdMenu { +private: + bool isDetailedView; + int lastRootMenuElement; + cForecastIO *forecastIO; + void SetRootMenu(void); + void SetDetailViewCurrent(void); + void SetDetailViewHourly(void); + void SetDetailViewDaily(void); +public: + cWeatherOsd(cForecastIO *forecastIO); + virtual ~cWeatherOsd(); + virtual eOSState ProcessKey(eKeys key); +}; + +#endif //__WEATHEROSD_H \ No newline at end of file -- cgit v1.2.3