text
stringlengths
0
357
pthread_mutex_unlock(&jobthread->mutex);
dc_jobthread_interrupt_idle(jobthread);
// wait until we're out of idle,
// after that the handle won't be in use anymore
while (1) {
pthread_mutex_lock(&jobthread->mutex);
if (jobthread->using_handle==0) {
pthread_mutex_unlock(&jobthread->mutex);
return;
}
pthread_mutex_unlock(&jobthread->mutex);
usleep(300*1000);
}
}
else
{
dc_log_info(jobthread->context, 0, "Unsuspending %s-thread.", jobthread->name);
pthread_mutex_lock(&jobthread->mutex);
jobthread->suspended = 0;
jobthread->idle_condflag = 1;
pthread_cond_signal(&jobthread->idle_cond);
pthread_mutex_unlock(&jobthread->mutex);
}
}
/*******************************************************************************
* the typical fetch, idle, interrupt-idle
******************************************************************************/
static int connect_to_imap(dc_jobthread_t* jobthread)
{
int ret_connected = DC_NOT_CONNECTED;
char* mvbox_name = NULL;
if(dc_imap_is_connected(jobthread->imap)) {
ret_connected = DC_ALREADY_CONNECTED;
goto cleanup;
}
if (!(ret_connected=dc_connect_to_configured_imap(jobthread->context, jobthread->imap))) {
goto cleanup;
}
if (dc_sqlite3_get_config_int(jobthread->context->sql, "folders_configured", 0)<DC_FOLDERS_CONFIGURED_VERSION) {
dc_configure_folders(jobthread->context, jobthread->imap, DC_CREATE_MVBOX);
}
mvbox_name = dc_sqlite3_get_config(jobthread->context->sql, jobthread->folder_config_name, NULL);
if (mvbox_name==NULL) {
dc_imap_disconnect(jobthread->imap);
ret_connected = DC_NOT_CONNECTED;
goto cleanup;
}
dc_imap_set_watch_folder(jobthread->imap, mvbox_name);
cleanup:
free(mvbox_name);
return ret_connected;
}
void dc_jobthread_fetch(dc_jobthread_t* jobthread, int use_network)
{
if (jobthread==NULL) {
return;
}
pthread_mutex_lock(&jobthread->mutex);
if (jobthread->suspended) {
pthread_mutex_unlock(&jobthread->mutex);
return;
}
jobthread->using_handle = 1;
pthread_mutex_unlock(&jobthread->mutex);
if (!use_network || jobthread->imap==NULL) {
goto cleanup;
}
clock_t start = clock();
if (!connect_to_imap(jobthread)) {
goto cleanup;
}
dc_log_info(jobthread->context, 0, "%s-fetch started...", jobthread->name);
dc_imap_fetch(jobthread->imap);
if (jobthread->imap->should_reconnect)
{
dc_log_info(jobthread->context, 0, "%s-fetch aborted, starting over...", jobthread->name);
dc_imap_fetch(jobthread->imap);
}