Index: mail/ChangeLog
===================================================================
--- mail/ChangeLog	(revision 35440)
+++ mail/ChangeLog	(working copy)
@@ -1,3 +1,19 @@
+2008-04-29  Milan Crha  <mcrha@redhat.com>
+
+	** Fix for bug #317755
+
+	* mail-send-recv.c: (refresh_folders_exec):
+	* mail-ops.c: (refresh_folder_desc):
+	Also sync with a server when refreshing folder.
+	* evolution-mail.schemas.in:
+	* mail-config.h: (mail_config_get_sync_timeout):
+	* mail-config.c: (mail_config_get_sync_timeout):
+	Use int value of /apps/evolution/mail/sync_interval where is set
+	how often propagate local changes to server.
+	* mail-component.c: (struct _MailComponentPrivate), (impl_dispose),
+	(mc_sync_store_done), (mc_sync_store), (call_mail_sync),
+	(mail_component_init): Upload local changes to server on some interval.
+
 2008-04-29  Milan Crha  <mcrha@redhat.com>
 
 	* em-utils.c: (em_utils_snoop_type):
Index: mail/mail-send-recv.c
===================================================================
--- mail/mail-send-recv.c	(revision 35440)
+++ mail/mail-send-recv.c	(working copy)
@@ -829,6 +829,8 @@ refresh_folders_exec (struct _refresh_fo
 	for (i=0;i<m->folders->len;i++) {
 		folder = mail_tool_uri_to_folder(m->folders->pdata[i], 0, &ex);
 		if (folder) {
+			camel_folder_sync (folder, FALSE, &ex);
+			camel_exception_clear(&ex);
 			camel_folder_refresh_info(folder, &ex);
 			camel_exception_clear(&ex);
 			camel_object_unref(folder);
Index: mail/mail-ops.c
===================================================================
--- mail/mail-ops.c	(revision 35440)
+++ mail/mail-ops.c	(working copy)
@@ -1620,7 +1620,10 @@ refresh_folder_desc (struct _sync_folder
 static void
 refresh_folder_exec (struct _sync_folder_msg *m)
 {
-	camel_folder_refresh_info(m->folder, &m->base.ex);
+	camel_folder_sync (m->folder, FALSE, &m->base.ex);
+
+	if (!camel_exception_is_set (&m->base.ex))
+		camel_folder_refresh_info(m->folder, &m->base.ex);
 }
 
 /* we just use the sync stuff where we can, since it would be the same */
Index: mail/evolution-mail.schemas.in
===================================================================
--- mail/evolution-mail.schemas.in	(revision 35440)
+++ mail/evolution-mail.schemas.in	(working copy)
@@ -1332,5 +1332,22 @@
       </locale>
     </schema>
 
+    <!-- Others -->
+
+    <schema>
+      <key>/schemas/apps/evolution/mail/sync_interval</key>
+      <applyto>/apps/evolution/mail/sync_interval</applyto>
+      <owner>evolution-mail</owner>
+      <type>int</type>
+      <default>60</default>
+      <locale name="C">
+        <short>Time interval, in seconds, how often upload store changes to server.</short>
+        <long>
+          Time interval, in seconds, how often upload store changes to server.
+	  The actual value cannot be less than 30 seconds.
+        </long>
+      </locale>
+    </schema>
+
   </schemalist>
 </gconfschemafile>
Index: mail/mail-component.c
===================================================================
--- mail/mail-component.c	(revision 35440)
+++ mail/mail-component.c	(working copy)
@@ -145,6 +145,9 @@ struct _MailComponentPrivate {
 	ELogger *logger;
 
 	EComponentView *component_view;
+
+	guint mail_sync_id; /* timeout id for sync call on the stores */
+	guint mail_sync_in_progress; /* is greater than 0 if still waiting to finish sync on some store */
 };
 
 /* indexed by _mail_component_folder_t */
@@ -452,6 +455,11 @@ impl_dispose (GObject *object)
 {
 	MailComponentPrivate *priv = MAIL_COMPONENT (object)->priv;
 
+	if (priv->mail_sync_id) {
+		g_source_remove (priv->mail_sync_id);
+		priv->mail_sync_id = 0;
+	}
+
 	view_changed_timeout_remove ((EComponentView *)object);
 
 	if (priv->activity_handler != NULL) {
@@ -1067,6 +1075,39 @@ impl_upgradeFromVersion (PortableServer_
 	camel_exception_clear (&ex);
 }
 
+static void
+mc_sync_store_done (CamelStore *store, void *data)
+{
+	MailComponent *mc = (MailComponent *) data;
+
+	mc->priv->mail_sync_in_progress--;
+}
+
+static void
+mc_sync_store (gpointer key, gpointer value, gpointer user_data)
+{
+	MailComponent *mc = (MailComponent *) user_data;
+
+	mc->priv->mail_sync_in_progress++;
+
+	mail_sync_store (CAMEL_STORE (key), FALSE, mc_sync_store_done, mc);
+}
+
+static gboolean
+call_mail_sync (gpointer user_data)
+{
+	extern int camel_application_is_exiting;
+	MailComponent *mc = (MailComponent *)user_data;
+
+	if (camel_application_is_exiting)
+		return FALSE;
+
+	if (!mc->priv->mail_sync_in_progress && session && camel_session_is_online (session))
+		mail_component_stores_foreach (mc, mc_sync_store, mc);
+
+	return !camel_application_is_exiting;
+}
+
 struct _setline_data {
 	GNOME_Evolution_Listener listener;
 	CORBA_boolean status;
@@ -1247,6 +1288,9 @@ mail_component_init (MailComponent *comp
 		(GDestroyNotify) store_hash_free);
 
 	mail_autoreceive_init();
+
+	priv->mail_sync_in_progress = 0;
+	priv->mail_sync_id = g_timeout_add_seconds (mail_config_get_sync_timeout (), call_mail_sync, component);
 }
 
 /* Public API.  */
Index: mail/mail-config.c
===================================================================
--- mail/mail-config.c	(revision 35440)
+++ mail/mail-config.c	(working copy)
@@ -667,6 +667,31 @@ mail_config_get_message_limit (void)
 	return config->mlimit_size;
 }
 
+/* timeout interval, in seconds, when to call server update */
+gint
+mail_config_get_sync_timeout (void)
+{
+	GConfClient *gconf = mail_config_get_gconf_client ();
+	gint res = 60;
+
+	if (gconf) {
+		GError *error = NULL;
+
+		res = gconf_client_get_int (gconf, "/apps/evolution/mail/sync_interval", &error);
+
+		/* do not allow recheck sooner than every 30 seconds */
+		if (error || res == 0)
+			res = 60;
+		else if (res < 30)
+			res = 30;
+
+		if (error)
+			g_error_free (error);
+	}
+
+	return res;
+}
+
 gboolean
 mail_config_get_enable_magic_spacebar ()
 {
Index: mail/mail-config.h
===================================================================
--- mail/mail-config.h	(revision 35440)
+++ mail/mail-config.h	(working copy)
@@ -154,6 +154,8 @@ char *mail_config_folder_to_safe_url (st
 guint mail_config_get_error_timeout  (void);
 guint mail_config_get_error_level  (void);
 
+gint mail_config_get_sync_timeout (void);
+
 void mail_config_reload_junk_headers (void);
 gboolean mail_config_get_lookup_book (void);
 gboolean mail_config_get_lookup_book_local_only (void);
