KWeather: improved fallback mechanism.

Signed-off-by: Mavridis Philippe <mavridisf@gmail.com>
Mavridis Philippe 2 years ago
parent 0dcd5cc1ae
commit d8a52e2665
Signed by: blu.256
GPG Key ID: F8D2D7E2F989A494

@ -1,59 +1,95 @@
#include <kstandarddirs.h>
#include <kiconloader.h> #include <kiconloader.h>
#include <kdebug.h>
#include "weather_icon.h" #include "weather_icon.h"
WeatherIcon::WeatherIcon( int condition, bool night ) WeatherIcon::WeatherIcon( int condition, bool night )
{ {
TQString name;
iconLoader = new TDEIconLoader("kweather"); iconLoader = new TDEIconLoader("kweather");
TQStringList fallback;
switch( condition ) switch( condition )
{ {
case Sunny: case Sunny:
{ {
name = "weather-clear"; if( night )
iconName = ( night ? name.append("-night") : name ); {
return; fallback << "weather-clear-night"; //xdg, kweather
}
fallback << "weather-clear"; // xdg, kweather
break;
} }
case Fog: case Fog:
{ {
name = "weather-fog";
if( night ) if( night )
{ {
TQString night_icon = name + "-night"; fallback << "weather-fog-night"; // themes, kweather
if( iconExists(night_icon) )
name = night_icon;
} }
iconName = name; fallback << "weather-fog"; // xdg, kweather
return;
break;
} }
case Mist: case Mist:
{ {
name = "weather-mist";
if( night ) if( night )
{ {
TQString night_icon = name + "-night"; fallback << "weather-mist-night"; // themes, kweather
if( iconExists(night_icon) ) }
name = night_icon; fallback << "weather-mist"; // themes, kweather
if( night )
{
fallback << "weather-fog-night"; // themes, kweather
} }
iconName = name; fallback << "weather-fog"; // xdg, kweather
return;
break;
}
case Overcast:
{
fallback << "weather-overcast"; // xdg, kweather
break;
} }
case Overcast: { iconName = "weather-overcast"; return; } case Hail:
case Hail: { iconName = "weather-freezing-rain"; return; } {
case LightRain: { iconName = "weather-showers-scattered"; return; } fallback << "weather-hail"; // themes
case Sleet: { iconName = "weather-snow-rain"; return; } fallback << "weather-freezing-rain"; // themes, kweather
fallback << "weather-snow"; // xdg, kweather
break;
}
case LightRain:
{
fallback << "weather-showers-scattered"; // xdg, kweather
break;
}
case Sleet:
{
fallback << "weather-snow-rain"; // themes, kweather
fallback << "weather-snow"; // xdg, kweather
break;
}
} }
iconName = findIcon(fallback);
return;
} }
WeatherIcon::WeatherIcon( int condition, bool night, unsigned int strength ) WeatherIcon::WeatherIcon( int condition, bool night, unsigned int strength )
{ {
iconLoader = new TDEIconLoader("kweather"); iconLoader = new TDEIconLoader("kweather");
TQString name; TQStringList fallback;
switch ( condition ) switch ( condition )
{ {
@ -61,120 +97,312 @@ WeatherIcon::WeatherIcon( int condition, bool night, unsigned int strength )
{ {
switch ( strength ) switch ( strength )
{ {
case 1: { name = "weather-few-clouds"; break; } case 1:
{
if( night )
{
fallback << "weather-few-clouds-night"; // xdg, kweather
}
fallback << "weather-few-clouds"; // xdg, kweather
break;
}
case 2: case 2:
{ {
name = "weather-moderate-clouds"; if( night )
if (! iconExists(name) )
{ {
name = "weather-few-clouds"; fallback << "weather-moderate-clouds-night"; // kweather
} }
fallback << "weather-moderate-clouds"; // kweather
if( night )
{
fallback << "weather-few-clouds-night"; // xdg, kweather
}
fallback << "weather-few-clouds"; // xdg, kweather
break; break;
} }
case 3: { name = "weather-clouds"; break; } case 3: {
if( night )
{
fallback << "weather-clouds-night"; // themes, kweather
}
fallback << "weather-clouds"; // themes, kweather
if( night )
{
fallback << "weather-few-clouds-night"; // xdg, kweather
}
fallback << "weather-few-clouds"; // xdg, kweather
break;
}
case 4: case 4:
{ {
name = "weather-ample-clouds"; if( night )
if (! iconExists(name) )
{ {
name = "weather-clouds"; fallback << "weather-ample-clouds-night"; // kweather
} }
fallback << "weather-ample-clouds"; // kweather
fallback << "weather-many-clouds"; // themes, kweather
fallback << "weather-overcast"; // xdg, kweather
break; break;
} }
case 5: { iconName = "weather-many-clouds"; return; } case 5: {
default: { iconName = "weather-clouds"; return; } fallback << "weather-many-clouds"; // themes, kweather
fallback << "weather-overcast"; // xdg, kweather
break;
}
default: {
fallback << "weather-clouds"; // themes, kweather
fallback << "weather-few-clouds"; // xdg, kweather
break;
}
} }
iconName = name.append( night ? "-night" : "" ); break;
return;
} }
case Showers: case Showers:
{ {
switch ( strength ) switch ( strength )
{ {
case 1: { name = "weather-showers-scattered"; break; } case 1:
case 2: { name = "weather-showers"; break; } {
if( night )
{
fallback << "weather-showers-scattered-night"; // themes, kweather
}
else
{
fallback << "weather-showers-scattered-day"; // themes, kweather
}
fallback << "weather-showers-scattered"; // xdg, kweather
break;
}
case 2:
{
if( night )
{
fallback << "weather-showers-night"; // themes, kweather
}
else
{
fallback << "weather-showers-day"; // themes, kweather
}
fallback << "weather-showers"; // xdg, kweather
break;
}
case 3: case 3:
default: { iconName = "weather-showers"; return; } default:
{
fallback << "weather-showers"; // xdg, kweather
break;
}
} }
iconName = name.append( night ? "-night" : "-day" ); break;
return;
} }
case Snow: case Snow:
{ {
switch( strength ) switch( strength )
{ {
case 1: { name = "weather-snow-scattered"; break; } case 1:
{
if( night )
{
fallback << "weather-snow-scattered-night"; // themes, kweather
}
else
{
fallback << "weather-snow-scattered-day"; // themes, kweather
}
fallback << "weather-snow-scattered"; // xdg, kweather
break;
}
case 2: case 2:
{ {
name = "weather-snow-moderate"; if( night )
TQString test_icon = name + "-day"; {
if (! iconExists(test_icon) ) fallback << "weather-snow-moderate-night"; // kweather
}
else
{ {
name = "weather-snow-scattered"; fallback << "weather-snow-moderate-day"; // kweather
} }
fallback << "weather-snow-moderate"; // kweather
if( night )
{
fallback << "weather-snow-scattered-night"; // themes, kweather
}
else
{
fallback << "weather-snow-scattered-day"; // themes, kweather
}
fallback << "weather-snow-scattered"; // xdg, kweather
break; break;
} }
case 3: case 3:
{ {
name = "weather-snow-ample"; if( night )
TQString test_icon = name + "-day"; {
if ( iconExists(test_icon) ) fallback << "weather-snow-ample-night"; // kweather
}
else
{ {
break; fallback << "weather-snow-ample-day"; // kweather
} }
fallback << "weather-snow-ample"; // kweather
fallback << "weather-snow"; // xdg, kweather
break;
} }
case 4: { iconName = "weather-snow-scattered"; return; }
case 4:
{
fallback << "weather-snow-scattered"; // xdg, kweather
break;
}
case 5: case 5:
default: { iconName = "weather-snow"; return; } default:
{
fallback << "weather-snow"; // xdg, kweather
break;
}
} }
iconName = name.append( night ? "-night" : "-day" ); break;
return;
} }
case Thunderstorm: case Thunderstorm:
switch ( strength ) switch ( strength )
{ {
case 1: { name = "weather-storm"; break; } case 1:
{
if( night )
{
fallback << "weather-storm-night"; // themes, kweather
}
else
{
fallback << "weather-storm-day"; // themes, kweather
}
fallback << "weather-storm"; // xdg, kweather
break;
}
case 2: case 2:
{ {
name = "weather-storm-moderate"; if( night )
TQString test_icon = name + "-day"; {
if (! iconExists(test_icon) ) fallback << "weather-storm-moderate-night"; // kweather
}
else
{
fallback << "weather-storm-moderate-day"; // kweather
}
fallback << "weather-storm-moderate"; // kweather
if( night )
{
fallback << "weather-storm-night"; // themes, kweather
}
else
{ {
name = "weather-storm"; fallback << "weather-storm-day"; // themes, kweather
} }
fallback << "weather-storm"; // xdg, kweather
break; break;
} }
case 3: case 3:
default: { iconName = "weather-storm"; return; } default:
{
fallback << "weather-storm"; // xdg, kweather
break;
}
} }
iconName = name.append( night ? "-night" : "-day" ); break;
return;
} }
iconName = findIcon(fallback);
return;
} }
WeatherIcon::~WeatherIcon() WeatherIcon::~WeatherIcon()
{} {}
TQString WeatherIcon::findIcon( TQStringList fallback )
{
// Check in theme
for ( TQStringList::Iterator icon = fallback.begin(); icon != fallback.end(); ++icon )
{
kdDebug() << "[WeatherIcon::findIcon] Searching for `" << *icon << "` in theme" << endl;
if( iconExists(*icon) )
{
kdDebug() << "[WeatherIcon::findIcon] FOUND `" << *icon << "` in theme" << endl;
return *icon;
}
}
// Check in kweather fallback
for ( TQStringList::Iterator icon = fallback.begin(); icon != fallback.end(); ++icon )
{
kdDebug() << "[WeatherIcon::findIcon] Searching for `" << *icon << "` in kweather icons" << endl;
if( iconExists(*icon, false) )
{
kdDebug() << "[WeatherIcon::findIcon] FOUND `" << *icon << "` in kweather icons" << endl;
return *icon;
}
}
return unknown();
}
bool WeatherIcon::iconExists( TQString& icon, bool inTheme ) bool WeatherIcon::iconExists( TQString& icon, bool inTheme )
{ {
if ( inTheme ) if( inTheme )
{ {
return iconLoader->theme()->iconPath(icon, TDEIcon::SizeMedium, TDEIcon::MatchExact).isValid(); return !( iconLoader->iconPath(icon, TDEIcon::Desktop, true).isNull() );
} }
else else
{ {
return !(iconLoader->iconPath(icon, TDEIcon::SizeMedium, true).isNull()); return !( locate( "data", "kweather/" + icon + ".png" ).isNull() );
} }
} }

@ -13,8 +13,9 @@ class WeatherIcon {
TQString& name() { return iconName; } TQString& name() { return iconName; }
private: private:
TQString findIcon( TQStringList fallback );
bool iconExists( TQString& icon, bool inTheme = true ); bool iconExists( TQString& icon, bool inTheme = true );
TDEIconLoader* iconLoader; TDEIconLoader* iconLoader;
TQString iconName = unknown(); TQString iconName;
}; };

@ -151,10 +151,8 @@ TQString WeatherService::iconFileName(const TQString &stationID)
{ {
TQString _name = m_weatherLib->iconName(stationID); TQString _name = m_weatherLib->iconName(stationID);
TQString icon = kapp->iconLoader()->iconPath(_name, TDEIcon::Desktop, true); TQString icon = kapp->iconLoader()->iconPath(_name, TDEIcon::Desktop, true);
kdDebug(12006) << "icon: " << icon << endl;
if( icon.isNull() ) if( icon.isNull() )
{ {
kdDebug(12006) << "fallback!" << endl;
icon = locate( "data", "kweather/" + _name + ".png" ); icon = locate( "data", "kweather/" + _name + ".png" );
} }
return icon; return icon;

Loading…
Cancel
Save