[chronojump] News working: download news and image (need to do improvements)
- From: Xavier de Blas <xaviblas src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [chronojump] News working: download news and image (need to do improvements)
- Date: Mon, 28 Dec 2020 13:48:09 +0000 (UTC)
commit 666fa05667f6816135abda99d05b0bdf0369e0f6
Author: Xavier de Blas <xaviblas gmail com>
Date: Mon Dec 28 14:47:42 2020 +0100
News working: download news and image (need to do improvements)
src/chronojump.cs | 1 +
src/gui/app1/chronojump.cs | 17 ++++++--
src/json/json.cs | 69 ++++++++++++++++++++++++++---
src/news.cs | 106 ++++++++++++++++++++++++++++++++++++---------
4 files changed, 163 insertions(+), 30 deletions(-)
---
diff --git a/src/chronojump.cs b/src/chronojump.cs
index 7d47ee95..0e02eab2 100644
--- a/src/chronojump.cs
+++ b/src/chronojump.cs
@@ -282,6 +282,7 @@ public class ChronoJump
UtilEncoder.CreateEncoderDirIfNeeded();
Util.CreateForceSensorDirIfNeeded();
Util.CreateRunEncoderDirIfNeeded();
+ News.CreateNewsDirIfNeeded();
//TODO: when a session is deleted, encoder data has to be deleted, also multimedia videos, I suppose. Show
message to user warning about it
//TODO: encoder weight auto written depending on person loaded, and changes if it changes person or weight
diff --git a/src/gui/app1/chronojump.cs b/src/gui/app1/chronojump.cs
index a37b92ab..d0e94b62 100644
--- a/src/gui/app1/chronojump.cs
+++ b/src/gui/app1/chronojump.cs
@@ -6768,14 +6768,25 @@ LogB.Debug("mc finished 5");
private void on_button_menu_news_clicked (object o, EventArgs args)
{
Json js = new Json();
- bool success = js.GetNews();
+ List<News> news_l = js.GetNews();
+
+ string strNews = "";
+ string sep = "";
+ foreach(News news in news_l)
+ {
+ strNews += sep + news.ToString();
+ sep = "\n";
+ }
+
+ if(news_l.Count >= 0)
+ {
+ News.InsertAndDownloadImageIfNeeded(js, news_l);
- if(success) {
LogB.Information(js.ResultMessage);
new DialogMessage(
"Chronojump",
Constants.MessageTypes.INFO,
- js.ResultMessage
+ strNews
);
}
else {
diff --git a/src/json/json.cs b/src/json/json.cs
index 55674939..e94a6019 100644
--- a/src/json/json.cs
+++ b/src/json/json.cs
@@ -184,21 +184,22 @@ public class Json
return true;
}
- public bool GetNews()
+ //get all the news, news class will decide if something have to be inserted or selected
+ public List<News> GetNews()
{
// Create a request using a URL that can receive a post.
if (! createWebRequest(requestType.GENERIC, "/getNews"))
- return false;
+ return new List<News>();
// Set the Method property of the request to GET.
request.Method = "GET";
// Set the ContentType property of the WebRequest.
- //request.ContentType = "application/x-www-form-urlencoded";
+ request.ContentType = "application/json; Charset=UTF-8";
HttpWebResponse response;
if(! getHttpWebResponse (request, out response, "Could not get last news."))
- return false;
+ return new List<News>();
string responseFromServer;
using (var sr = new StreamReader(response.GetResponseStream()))
@@ -206,12 +207,68 @@ public class Json
responseFromServer = sr.ReadToEnd();
}
- string str = responseFromServer;
+ this.ResultMessage = responseFromServer;
- this.ResultMessage = str;
+ return newsDeserialize(responseFromServer);
+ }
+ private List<News> newsDeserialize(string str)
+ {
+ LogB.Information("newsDeserialize:|" + str + "|");
+
+ List<News> news_l = new List<News>();
+ JsonValue jsonNewsAll = JsonValue.Parse(str);
+
+ foreach (JsonValue jsonNews in jsonNewsAll)
+ {
+ Int32 code = jsonNews ["code"];
+ Int32 category = jsonNews ["category"];
+ Int32 version = jsonNews ["version"];
+ string titleEn = jsonNews ["titleEn"];
+ string titleEs = jsonNews ["titleEs"];
+ string linkEn = jsonNews ["linkEn"];
+ string linkEs = jsonNews ["linkEs"];
+ string descriptionEn = jsonNews ["descriptionEn"];
+ string descriptionEs = jsonNews ["descriptionEs"];
+ string linkServerImage = jsonNews ["linkImage"];
+ LogB.Information("Deserialize linkServerImage: " + linkServerImage);
+
+ news_l.Add(new News(code, category, version, false,
+ titleEn, titleEs, linkEn, linkEs, descriptionEn,
descriptionEs, linkServerImage));
+ }
+
+ return news_l;
+ }
+
+ //can be a jpeg or a png
+ public bool DownloadNewsImage(string imageServerUrl, int code)
+ {
+ LogB.Information("imageServerUrl:");
+ LogB.Information(imageServerUrl);
+ string extension = "";
+ if(Util.IsJpeg(imageServerUrl))
+ extension = ".jpg";
+ else if (Util.IsPng(imageServerUrl))
+ extension = ".png";
+ else
+ return false;
+
+ try {
+ string copyTo = Path.Combine(News.GetNewsDir(), code.ToString() + extension);
+ using (WebClient client = new WebClient())
+ {
+ LogB.Information (string.Format("News DownloadImage from: {0} to: {1}",
+ imageServerUrl, copyTo));
+
+ client.DownloadFile(new Uri(imageServerUrl), copyTo); //if exists, it
overwrites
+ }
+ } catch {
+ LogB.Warning("DownloadImage catched");
+ return false;
+ }
return true;
}
+
/*
* if software just started, ping gets stuck by network problems, and user try to exit software,
* thread.Abort doesn't kill the thread properly
diff --git a/src/news.cs b/src/news.cs
index 03da81cf..e7cb1297 100644
--- a/src/news.cs
+++ b/src/news.cs
@@ -25,54 +25,118 @@ using Mono.Unix;
public class News
{
- private int uniqueID;
private int code; //regular integer
private int category; //at the moment: 0 software, 1 products
private int version; //is NOT version of the software, is version of the news, because can be updated
with new text or image. New version of the sofware will be simply a new code
- private string versionDateTime;
private bool viewed;
- private string title;
- private string link;
- private string description;
+ //En and Es because there are two versions of the web site
+ private string titleEn;
+ private string titleEs;
+ private string linkEn;
+ private string linkEs;
+ private string descriptionEn;
+ private string descriptionEs;
+ private string linkServerImage; //stored to download again if changed (future)
/* constructors */
- //have a uniqueID -1 contructor, useful when set is deleted
+ //have a code -1 contructor
public News()
{
- uniqueID = -1;
+ code = -1;
}
//constructor
- public News(int uniqueID, int code, int category, int version, string versionDateTime,
- bool viewed, string title, string link, string description)
+ public News(int code, int category, int version, bool viewed,
+ string titleEn, string titleEs, string linkEn, string linkEs,
+ string descriptionEn, string descriptionEs, string linkServerImage)
{
- this.uniqueID = uniqueID;
this.code = code;
this.category = category;
this.version = version;
- this.versionDateTime = versionDateTime;
this.viewed = viewed;
- this.title = title;
- this.link = link;
- this.description = description;
+ this.titleEn = titleEn;
+ this.titleEs = titleEs;
+ this.linkEn = linkEn;
+ this.linkEs = linkEs;
+ this.descriptionEn = descriptionEn;
+ this.descriptionEs = descriptionEs;
+ this.linkServerImage = linkServerImage;
}
- /* methods */
+ /* public methods */
+
+ public override string ToString()
+ {
+ return string.Format("{0}:{1}:{2}:{3}:{4}:{5}:{6}:{7}:{8}:{9}:{10}",
+ code, category, version, viewed, titleEn, titleEs, linkEn, linkEs,
descriptionEn, descriptionEs, linkServerImage);
+ }
public int InsertSQL(bool dbconOpened)
{
return SqliteNews.Insert(dbconOpened, toSQLInsertString());
}
+
+ /* public static methods */
+
+ public static void InsertAndDownloadImageIfNeeded(Json js, List<News> newsAtServer_l)
+ {
+ List<News> newsAtDB_l = SqliteNews.Select(false, -1);
+ foreach(News nAtServer in newsAtServer_l)
+ {
+ bool found = false;
+ foreach(News nAtDB in newsAtDB_l)
+ if(nAtServer.Code == nAtDB.Code)
+ {
+ found = true;
+ break;
+ }
+ if(! found)
+ {
+ nAtServer.InsertSQL(false);
+ js.DownloadNewsImage(nAtServer.LinkServerImage, nAtServer.Code);
+ }
+ }
+ }
+
+ public static string GetNewsDir()
+ {
+ return Path.Combine(
+ Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
+ "Chronojump" + Path.DirectorySeparatorChar + "news");
+ }
+ public static void CreateNewsDirIfNeeded ()
+ {
+ string dir = GetNewsDir();
+ if( ! Directory.Exists(dir)) {
+ Directory.CreateDirectory (dir);
+ LogB.Information ("created dir:", dir);
+ }
+ }
+
+ /* private methods */
+
private string toSQLInsertString()
{
- string uniqueIDStr = "NULL";
- if(uniqueID != -1)
- uniqueIDStr = uniqueID.ToString();
+ string codeStr = "NULL";
+ if(code != -1)
+ codeStr = code.ToString();
- return uniqueIDStr + ", " + code + ", " + category + ", " +
- version + ", \"" + versionDateTime + "\", " + Util.BoolToInt(viewed).ToString() + ",
\"" +
- title + "\", \"" + link + "\", \"" + description + "\"";
+ return codeStr + ", " + category + ", " + version + ", " +
+ Util.BoolToInt(viewed).ToString() + ", \"" +
+ titleEn + "\", \"" + titleEs + "\", \"" +
+ linkEn + "\", \"" + linkEs + "\", \"" +
+ descriptionEn + "\", \"" + descriptionEs + "\", \"" +
+ linkServerImage + "\"";
}
+ public int Code
+ {
+ get { return code; }
+ }
+
+ public string LinkServerImage
+ {
+ get { return linkServerImage; }
+ }
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]