Update of /cvsroot/mahogany/M/src/mail
In directory usw-pr-cvs1:/tmp/cvs-serv28764/src/mail

Modified Files:
        MailFolderCC.cpp 
Log Message:
reuse the connection in CheckStatus() if possible

Index: MailFolderCC.cpp
===================================================================
RCS file: /cvsroot/mahogany/M/src/mail/MailFolderCC.cpp,v
retrieving revision 1.620
retrieving revision 1.621
diff -b -u -2 -r1.620 -r1.621
--- MailFolderCC.cpp    4 Jun 2002 13:54:34 -0000       1.620
+++ MailFolderCC.cpp    4 Jun 2002 19:43:02 -0000       1.621
@@ -203,18 +203,7 @@
 
 // ----------------------------------------------------------------------------
-// private functions
+// typedefs
 // ----------------------------------------------------------------------------
 
-extern "C"
-{
-#ifdef USE_READ_PROGRESS
-   void mahogany_read_progress(GETS_DATA *md, unsigned long count);
-#endif
-
-#ifdef USE_BLOCK_NOTIFY
-   void *mahogany_block_notify(int reason, void *data);
-#endif
-};
-
 typedef void (*mm_list_handler)(MAILSTREAM *stream,
                                 char delim,
@@ -226,7 +215,4 @@
                                   MAILSTATUS *status);
 
-// return the c-client "{...}" string for the given folder
-static wxString GetImapSpec(const MFolder *folder);
-
 // ----------------------------------------------------------------------------
 // globals
@@ -263,7 +249,44 @@
 #endif
 
-// loglevel for cclient error messages:
+/// loglevel for cclient error messages:
 static int cc_loglevel = wxLOG_Error;
 
+// ----------------------------------------------------------------------------
+// private functions
+// ----------------------------------------------------------------------------
+
+extern "C"
+{
+#ifdef USE_READ_PROGRESS
+   void mahogany_read_progress(GETS_DATA *md, unsigned long count);
+#endif
+
+#ifdef USE_BLOCK_NOTIFY
+   void *mahogany_block_notify(int reason, void *data);
+#endif
+};
+
+/// return the c-client "{...}" string for the given folder
+static wxString GetImapSpec(const MFolder *folder);
+
+/// trivial wrappers around mail_open() which wants a non const "char *" (ugh)
+static inline
+MAILSTREAM *MailOpen(MAILSTREAM *stream, const char *mailbox, long options = 0)
+{
+   if ( mm_show_debug )
+      options |= OP_DEBUG;
+
+   return mail_open(stream, (char *)mailbox, options);
+}
+
+// we want to specify the stream as the first argument as c-client does so we
+// can't use just one function with default NIL parameter for the stream but
+// instead we have this second version for the most common case
+static inline
+MAILSTREAM *MailOpen(const char *mailbox, long options = 0)
+{
+   return MailOpen(NIL, mailbox, options);
+}
+
 // ============================================================================
 // private classes
@@ -429,5 +452,5 @@
 
 private:
-   // ctor is private, nobody except GetFolderServer() can create us
+   // ctor is private, nobody except GetOrCreate() can create us
    ServerInfoEntry(const MFolder *folder, const NETMBX& netmbx);
 
@@ -2076,6 +2099,5 @@
 
       CCErrorDisabler noErrs;
-      stream = mail_open(NULL, (char *)imapspec.c_str(),
-                         mm_show_debug ? OP_DEBUG : NIL);
+      stream = MailOpen(imapspec);
    }
 
@@ -2109,6 +2131,5 @@
                  imapspec.c_str());
 
-      stream = mail_open(stream, (char *)imapspec.c_str(),
-                         mm_show_debug ? OP_DEBUG : NIL);
+      stream = MailOpen(stream, imapspec);
    }
 
@@ -2501,5 +2522,5 @@
       CCDefaultFolder def(this);
 
-      long ccOptions = mm_show_debug ? OP_DEBUG : 0;
+      long ccOptions = 0;
       bool tryOpen = true;
       switch ( openmode )
@@ -2549,6 +2570,6 @@
                     m_ImapSpec.c_str());
 
-         m_MailStream = mail_open(server ? server->GetStream() : NIL,
-                                  (char *)m_ImapSpec.c_str(), ccOptions);
+         m_MailStream = MailOpen(server ? server->GetStream() : NIL,
+                                 m_ImapSpec, ccOptions);
       }
    } // end of cclient lock block
@@ -2578,10 +2599,6 @@
       // redirect all notifications to us again
       CCDefaultFolder def(this);
-      MAILSTREAM *msHalfOpened = mail_open
-                                 (
-                                  m_MailStream,
-                                  (char *)m_ImapSpec.c_str(),
-                                  (mm_show_debug ? OP_DEBUG : 0) | OP_HALFOPEN
-                                 );
+      MAILSTREAM *
+         msHalfOpened = MailOpen(m_MailStream, m_ImapSpec, OP_HALFOPEN);
       if ( msHalfOpened )
       {
@@ -3280,4 +3297,108 @@
 
 /* static */
+bool
+MailFolderCC::DoCheckStatus(const MFolder *folder, MAILSTATUS *mailstatus)
+{
+   static const int STATUS_FLAGS = SA_MESSAGES | SA_RECENT | SA_UNSEEN;
+
+   wxBusyCursor busyCursor;
+
+   // instead of calling mail_status() with NIL stream we always open the
+   // connection to the folder manually before as this gives us a
+   // possibility to reuse it for the subsequent operations: this is
+   // especially important when the user chooses to update the status of the
+   // entire subtree as then CheckStatus() is called many times in quick
+   // succession and opening a new connection each time is very inefficient
+
+   // reuse an existing connection to the server if we have one
+   ServerInfoEntry *server;
+   if ( IsReusableFolder(folder) )
+   {
+      // look for the existing entry for this server
+      server = ServerInfoEntry::GetOrCreate(folder);
+
+      if ( !server )
+      {
+         return false;
+      }
+   }
+   else
+   {
+      server = NULL;
+   }
+
+   MAILSTREAM *stream;
+   if ( server )
+   {
+      stream = server->GetStream();
+   }
+   else // no connection to reuse
+   {
+      stream = NULL;
+   }
+
+   // find out the login and password we need: first see if we don't already
+   // have them
+   String login, password;
+   bool hasAuthInfo = server && server->GetAuthInfo(login, password);
+   if ( !hasAuthInfo )
+   {
+      login = folder->GetLogin();
+      password = folder->GetPassword();
+
+      if ( !GetAuthInfoForFolder(folder, login, password ) )
+      {
+         return false;
+      }
+   }
+
+   SetLoginData(login, password);
+
+   // preopen the stream if it may be reused, as explained above
+   String spec = MailFolder::GetImapSpec(folder->GetType(),
+                                         folder->GetFlags(),
+                                         folder->GetPath(),
+                                         folder->GetServer(),
+                                         login);
+
+   if ( server && !stream )
+   {
+      // we're not interested in mm_exists() and what not
+      CCCallbackDisabler noCallbacks;
+
+      stream = MailOpen(spec, OP_HALFOPEN | OP_READONLY);
+      if ( !stream )
+      {
+         // if we failed to open it, checking its status won't work neither
+         return false;
+      }
+   }
+
+   // finally call mail_status()
+   MMStatusRedirector statusRedir(spec, mailstatus);
+
+   wxLogTrace(TRACE_MF_CALLS, "MailFolderCC::CheckStatus() on %s.",
+              spec.c_str());
+
+   mail_status(stream, (char *)spec.c_str(), STATUS_FLAGS);
+
+   // keep the stream alive to be reused in the next call, if any
+   if ( server )
+   {
+      server->KeepStream(stream, folder);
+
+      // and also remember the auth params if we hadn't had them before
+      if ( !hasAuthInfo )
+      {
+         server->SetAuthInfo(login, password);
+      }
+   }
+
+   // we succeed only if we managed to get the values of all flags included in
+   // STATUS_FLAGS
+   return (mailstatus->flags & STATUS_FLAGS) == STATUS_FLAGS;
+}
+
+/* static */
 bool MailFolderCC::CheckStatus(const MFolder *folder)
 {
@@ -3311,37 +3432,7 @@
    (void)mfStatusCache->GetStatus(folder->GetFullName(), &status);
 
-   static const int STATUS_FLAGS = SA_MESSAGES | SA_RECENT | SA_UNSEEN;
-
    // and check for the new one
    MAILSTATUS mailstatus;
-   {
-      wxBusyCursor busyCursor;
-
-      String login = folder->GetLogin(),
-             password = folder->GetPassword();
-
-      String spec = MailFolder::GetImapSpec(folder->GetType(),
-                                            folder->GetFlags(),
-                                            folder->GetPath(),
-                                            folder->GetServer(),
-                                            login);
-
-      if ( !GetAuthInfoForFolder(folder, login, password ) )
-      {
-         return false;
-      }
-
-      SetLoginData(login, password);
-
-      MMStatusRedirector statusRedir(spec, &mailstatus);
-
-      wxLogTrace(TRACE_MF_CALLS, "MailFolderCC::CheckStatus() on %s.",
-                 spec.c_str());
-
-      mail_status(NULL, (char *)spec.c_str(), STATUS_FLAGS);
-   }
-
-   // did we succeed?
-   if ( (mailstatus.flags & STATUS_FLAGS) != STATUS_FLAGS )
+   if ( !DoCheckStatus(folder, &mailstatus) )
    {
       ERRORMESSAGE(( _("Failed to check status of the folder '%s'"),
@@ -6088,6 +6179,5 @@
       // open the folder: although we don't need to do it to get its status, we
       // have to do it anyhow below, so better do it right now
-      stream = mail_open(stream, (char *)mboxpath.c_str(),
-                         mm_show_debug ? OP_DEBUG : NIL);
+      stream = MailOpen(stream, mboxpath);
 
       if ( !stream )
@@ -6223,6 +6313,5 @@
    MAILSTREAM *stream = server ? server->GetStream() : NIL;
 
-   stream = mail_open(stream, (char *)imapSpec.c_str(),
-                      (mm_show_debug ? OP_DEBUG : NIL) | OP_HALFOPEN);
+   stream = MailOpen(stream, imapSpec, OP_HALFOPEN);
 
    if ( stream != NIL )
@@ -6644,4 +6733,7 @@
    {
       // invalid remote spec?
+      ERRORMESSAGE(( _("Parameters of the folder '%s' are invalid."),
+                     folder->GetFullName().c_str() ));
+
       return NULL;
    }


_______________________________________________________________

Don't miss the 2002 Sprint PCS Application Developer's Conference
August 25-28 in Las Vegas -- http://devcon.sprintpcs.com/adp/index.cfm

_______________________________________________
Mahogany-cvsupdates mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/mahogany-cvsupdates

Reply via email to