Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

start working on themes #479

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 39 additions & 47 deletions src/ArticleTheme.vala
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,20 @@
// along with FeedReader. If not, see <http://www.gnu.org/licenses/>.
using Gee;

public struct ThemeInfo {
string name;
string author;
string path;
}

public class FeedReader.ArticleTheme {
private static ArrayList<HashMap<string, string>> ? themes = null;
private static HashMap<string, ThemeInfo?> ? themes = null;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the second type argument ThemeInfo?? Are there cases where we would want them theme to null?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not at all, it's just valac complained about not using themeInfo?



public static ArrayList<HashMap> getThemes(){
public static HashMap<string, ThemeInfo?> getThemes(){
if(themes == null){
// Local themes
themes = new ArrayList<HashMap<string, string>> ();
themes = new HashMap<string, ThemeInfo?> ();
string local_dir = GLib.Environment.get_user_data_dir() + "/feedreader/themes/";
grabThemes(local_dir);
// Global themes
Expand All @@ -31,50 +37,37 @@ public class FeedReader.ArticleTheme {
return themes;
}

private static HashMap<string, string> getThemeInfo (string theme_path) {
var themeInfo = new HashMap<string, string> ();
bool corrupted_theme = true;
string theme_name = "";
string author = "";
private static ThemeInfo? getTheme (string theme_path) {
var themeInfo = ThemeInfo ();
bool corrupted_theme = false;
try {
Dir theme_dir = Dir.open(theme_path, 0);
string ? name = null;
while ((name = theme_dir.read_name()) != null){
if (name == "theme.json"){
corrupted_theme = false;
string path = Path.build_filename(theme_path, name);
Json.Parser parser = new Json.Parser();
try {
parser.load_from_file(path);
Json.Object obj = parser.get_root().get_object();
string path = Path.build_filename(theme_path, "theme.json");
Json.Parser parser = new Json.Parser();
parser.load_from_file(path);
Json.Object obj = parser.get_root().get_object();

Value val;
foreach (unowned string nname in obj.get_members()){
val = obj.get_member(nname).get_value();
switch(nname) {
case "author":
author = (string) val;
break;
case "name":
theme_name = (string) val;
break;
}
}
}catch(Error err){
corrupted_theme = true;
}
Value val;
foreach (unowned string node_name in obj.get_members()){
val = obj.get_member(node_name).get_value();
switch(node_name) {
case "author":
themeInfo.author = (string) val;
break;
case "name":
themeInfo.name = (string) val;
break;
}
}
}catch(GLib.FileError err){
themeInfo.path = theme_path;

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logging something here would be nice too.

} catch(GLib.FileError err){
Logger.error("A theme must be corrupted :" + theme_path);
corrupted_theme = true;
}
if (!corrupted_theme){
themeInfo.set("name", theme_name);
themeInfo.set("author", author);
themeInfo.set("path", theme_path);
} else {
themeInfo.set("corrupted", "true");
}

if (corrupted_theme)
return null;

return themeInfo;
}

Expand All @@ -85,18 +78,17 @@ public class FeedReader.ArticleTheme {
while ((name = dir.read_name()) != null){
string path = Path.build_filename(location, name);
if(FileUtils.test(path, FileTest.IS_DIR)){
var themeInfo = getThemeInfo(path);
if (themeInfo.has_key("corrupted") == false){
themes.add(themeInfo);
}
var themeInfo = getTheme(path);
if(themeInfo != null)
themes.set(name, themeInfo);
}
}
} catch (GLib.FileError err){

Logger.debug("Couldn't reach the location of themes : " + location);
}
}

public static bool isExists(string theme_location){
public static bool exists(string theme_location){
// Check wether a theme exists or not
bool exists = true;
try {
Expand Down
2 changes: 1 addition & 1 deletion src/UtilsUI.vala
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public class FeedReader.UtilsUI : GLib.Object {
string css = "";
try
{
if (ArticleTheme.isExists(theme) == false) {
if (!ArticleTheme.exists(theme)) {
// only when none of the themes are found!
uint8[] templateContents;
var htmlFile = File.new_for_uri("resource:///org/gnome/FeedReader/ArticleView/default/article.html");
Expand Down
14 changes: 7 additions & 7 deletions src/Widgets/Setting.vala
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,23 @@ public class FeedReader.SettingFont : FeedReader.Setting {

public class FeedReader.ArticleThemeSetting : FeedReader.Setting {

public ArticleThemeSetting (string name, GLib.Settings settings, string key, ArrayList<HashMap> ? themes = null, string ? tooltip = null){
public ArticleThemeSetting (string name, GLib.Settings settings, string key, HashMap<string, ThemeInfo?> ? themes = null, string ? tooltip = null){
base (name, tooltip);
if (themes != null) {
var liststore = new Gtk.ListStore(2, typeof(string), typeof(string));
int active = 0;
bool was_found = false;
string current_theme = settings.get_string(key);
foreach(HashMap<string, string> theme in themes) {

foreach(ThemeInfo theme in themes.values) {
Gtk.TreeIter iter;
string theme_name = theme.get("name");
string path = theme.get("path");
if (current_theme == path){

if (current_theme == theme.path){
was_found = true;
}
liststore.append(out iter);
liststore.set(iter, 0, theme_name);
liststore.set(iter, 1, path);
liststore.set(iter, 0, theme.name);
liststore.set(iter, 1, theme.path);
if(!was_found){
active += 1;
}
Expand Down