[chronojump/optimizeRFD] News working at start (inside pingThread) managed by a pulse
- From: Xavier Padullés <xpadulles src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [chronojump/optimizeRFD] News working at start (inside pingThread) managed by a pulse
- Date: Sun, 3 Jan 2021 17:07:55 +0000 (UTC)
commit 7f622e07d01b9e1be22f8f5f5bd921ddc71c6b1e
Author: Xavier de Blas <xaviblas gmail com>
Date: Mon Dec 28 19:47:46 2020 +0100
News working at start (inside pingThread) managed by a pulse
src/gui/app1/chronojump.cs | 102 +++++++++++++++++++--------------------------
src/gui/app1/icons.cs | 4 ++
src/json/json.cs | 34 ++++++++-------
src/news.cs | 29 +++++++++++--
4 files changed, 90 insertions(+), 79 deletions(-)
---
diff --git a/src/gui/app1/chronojump.cs b/src/gui/app1/chronojump.cs
index d0e94b625..be467a917 100644
--- a/src/gui/app1/chronojump.cs
+++ b/src/gui/app1/chronojump.cs
@@ -416,6 +416,8 @@ public partial class ChronoJumpWindow
private static ReactionTimeType currentReactionTimeType;
private static MultiChronopicType currentMultiChronopicType;
private static Report report;
+ private static List<News> newsAtDB_l; //to not read/write SQL on pingThread and at the same time
outside of thread
+ private static List<News> newsAtServer_l; //to not read/write SQL on pingThread and at the same time
outside of thread
//windows needed
ChronopicRegisterWindow chronopicRegisterWin;
@@ -710,7 +712,10 @@ public partial class ChronoJumpWindow
if( ! configChronojump.Compujump)
{
LogB.Information("Ping thread will start");
- pingThread = new Thread (new ThreadStart (pingAtStart));
+
+ newsAtDB_l = SqliteNews.Select(false, -1);
+ pingThread = new Thread (new ThreadStart (pingAndNewsAtStart));
+ GLib.Idle.Add (new GLib.IdleHandler (pulsePingAndNewsGTK));
pingThread.Start();
} else
LogB.Information("Ping discarded (Compujump)");
@@ -6767,82 +6772,59 @@ LogB.Debug("mc finished 5");
private void on_button_menu_news_clicked (object o, EventArgs args)
{
- Json js = new Json();
- List<News> news_l = js.GetNews();
-
- string strNews = "";
- string sep = "";
- foreach(News news in news_l)
- {
- strNews += sep + news.ToString();
- sep = "\n";
- }
+ getNews();
+ }
- if(news_l.Count >= 0)
+ private bool pulsePingAndNewsGTK ()
+ {
+ if(! pingThread.IsAlive)
{
- News.InsertAndDownloadImageIfNeeded(js, news_l);
+ if(News.InsertIfNeeded (newsAtDB_l, newsAtServer_l))
+ {
+ Pixbuf pixbuf = new Pixbuf (null, Util.GetImagePath(false) +
"image_store_news.png");
+ image_menu_news.Pixbuf = pixbuf;
+ image_menu_news1.Pixbuf = pixbuf;
+ }
- LogB.Information(js.ResultMessage);
- new DialogMessage(
- "Chronojump",
- Constants.MessageTypes.INFO,
- strNews
- );
- }
- else {
- LogB.Error(js.ResultMessage);
- new DialogMessage(
- "Chronojump",
- Constants.MessageTypes.WARNING,
- js.ResultMessage);
+ LogB.Information("pulsePingAndNews ending here");
+ LogB.ThreadEnded();
+ return false;
}
- }
- /*
- private void on_menuitem_ping_activate (object o, EventArgs args)
- {
- pingDo(true);
+ Thread.Sleep (250);
+ //Log.Write(" (PulseGTK:" + thread.ThreadState.ToString() + ") ");
+ return true;
}
- */
- private void pingAtStart()
+
+ //declared here in order to be easy closed on exit Chronojump
+ Json jsPing;
+ private void pingAndNewsAtStart()
{
- pingDo(false);
+ jsPing = new Json();
+ if(pingDo())
+ getNews();
}
- //declared here in order to be easy closed on exit
- Json jsPing;
- private void pingDo(bool showInWindow)
+ private bool pingDo()
{
LogB.Information("version at pingDo:" + UtilAll.ReadVersionFromBuildInfo());
- jsPing = new Json();
bool success = jsPing.Ping(UtilAll.GetOS(), UtilAll.ReadVersionFromBuildInfo(),
preferences.machineID);
- if(success) {
+ if(success)
LogB.Information(jsPing.ResultMessage);
- if(showInWindow)
- new DialogMessage(
- "Chronojump",
- Constants.MessageTypes.INFO,
- jsPing.ResultMessage);
- }
- else {
+ else
LogB.Error(jsPing.ResultMessage);
- if(showInWindow)
- new DialogMessage(
- "Chronojump",
- Constants.MessageTypes.WARNING,
- jsPing.ResultMessage);
- }
- /*
- new DialogMessage(
- "Chronojump",
- Constants.MessageTypes.INFO,
- "Temporarily Disabled");
- */
+ return success;
+
}
-
-
+
+ private void getNews()
+ {
+ LogB.Information("getNews()");
+ newsAtServer_l = jsPing.GetNews();
+ }
+
private void on_preferences_debug_mode_start (object o, EventArgs args) {
//first delete debug file
Util.FileDelete(System.IO.Path.GetTempPath() + "chronojump-debug.txt");
diff --git a/src/gui/app1/icons.cs b/src/gui/app1/icons.cs
index 212ba061c..8c6847f74 100644
--- a/src/gui/app1/icons.cs
+++ b/src/gui/app1/icons.cs
@@ -248,6 +248,10 @@ public partial class ChronoJumpWindow
image_contacts_exercise_settings.Pixbuf = pixbuf;
image_encoder_exercise_settings.Pixbuf = pixbuf;
+ pixbuf = new Pixbuf (null, Util.GetImagePath(false) + "image_store.png");
+ image_menu_news.Pixbuf = pixbuf;
+ image_menu_news1.Pixbuf = pixbuf;
+
//pixbuf = new Pixbuf (null, Util.GetImagePath(false) + "image_minimize.png");
//image_minimize.Pixbuf = pixbuf;
//image_minimize1.Pixbuf = pixbuf;
diff --git a/src/json/json.cs b/src/json/json.cs
index e94a60196..8884326b3 100644
--- a/src/json/json.cs
+++ b/src/json/json.cs
@@ -185,6 +185,7 @@ public class Json
}
//get all the news, news class will decide if something have to be inserted or selected
+ //called by pingThread at start
public List<News> GetNews()
{
// Create a request using a URL that can receive a post.
@@ -220,6 +221,7 @@ public class Json
foreach (JsonValue jsonNews in jsonNewsAll)
{
+ // 1) create news from json
Int32 code = jsonNews ["code"];
Int32 category = jsonNews ["category"];
Int32 version = jsonNews ["version"];
@@ -230,36 +232,36 @@ public class Json
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));
+
+ // 2) download image
+ //if image does not exist, download here (in pingThread)
+ //can be a jpeg or a png
+ string extension = "";
+ if(Util.IsJpeg(linkServerImage))
+ extension = ".jpg";
+ else if (Util.IsPng(linkServerImage))
+ extension = ".png";
+
+ string copyTo = Path.Combine(News.GetNewsDir(), code.ToString() + extension);
+ if(! File.Exists(copyTo))
+ downloadNewsImage(linkServerImage, copyTo);
}
return news_l;
}
- //can be a jpeg or a png
- public bool DownloadNewsImage(string imageServerUrl, int code)
+ private bool downloadNewsImage(string linkServerImage, string copyTo)
{
- 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));
+ linkServerImage, copyTo));
- client.DownloadFile(new Uri(imageServerUrl), copyTo); //if exists, it
overwrites
+ client.DownloadFile(new Uri(linkServerImage), copyTo); //if exists, it
overwrites
}
} catch {
LogB.Warning("DownloadImage catched");
diff --git a/src/news.cs b/src/news.cs
index e480c6879..a8a64946d 100644
--- a/src/news.cs
+++ b/src/news.cs
@@ -78,9 +78,31 @@ public class News
/* public static methods */
- public static void InsertAndDownloadImageIfNeeded(Json js, List<News> newsAtServer_l)
+ /*
+ //this method does not use SQL, called by pingThread
+ public static bool AnythingNew (List<News> newsAtDB_l, 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)
+ return true;
+ }
+
+ return false;
+ }
+ */
+
+ //this method uses SQL, called by main thread
+ public static bool InsertIfNeeded(List<News> newsAtDB_l, List<News> newsAtServer_l)
+ {
+ bool newStuff = false;
foreach(News nAtServer in newsAtServer_l)
{
bool found = false;
@@ -93,9 +115,10 @@ public class News
if(! found)
{
nAtServer.InsertSQL(false);
- js.DownloadNewsImage(nAtServer.LinkServerImage, nAtServer.Code);
+ newStuff = true;
}
}
+ return newStuff;
}
public static string GetNewsDir()
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]