text
stringlengths
0
357
/*******************************************************************************
* Import/Export Thread and Main Interface
******************************************************************************/
/**
* Import/export things.
* For this purpose, the function creates a job that is executed in the IMAP-thread then;
* this requires to call dc_perform_imap_jobs() regularly.
*
* What to do is defined by the _what_ parameter which may be one of the following:
*
* - **DC_IMEX_EXPORT_BACKUP** (11) - Export a backup to the directory given as `param1`.
* The backup contains all contacts, chats, images and other data and device independent settings.
* The backup does not contain device dependent settings as ringtones or LED notification settings.
* The name of the backup is typically `delta-chat.<day>.bak`, if more than one backup is create on a day,
* the format is `delta-chat.<day>-<number>.bak`
*
* - **DC_IMEX_IMPORT_BACKUP** (12) - `param1` is the file (not: directory) to import. The file is normally
* created by DC_IMEX_EXPORT_BACKUP and detected by dc_imex_has_backup(). Importing a backup
* is only possible as long as the context is not configured or used in another way.
*
* - **DC_IMEX_EXPORT_SELF_KEYS** (1) - Export all private keys and all public keys of the user to the
* directory given as `param1`. The default key is written to the files `public-key-default.asc`
* and `private-key-default.asc`, if there are more keys, they are written to files as
* `public-key-<id>.asc` and `private-key-<id>.asc`
*
* - **DC_IMEX_IMPORT_SELF_KEYS** (2) - Import private keys found in the directory given as `param1`.
* The last imported key is made the default keys unless its name contains the string `legacy`. Public keys are not imported.
*
* While dc_imex() returns immediately, the started job may take a while,
* you can stop it using dc_stop_ongoing_process(). During execution of the job,
* some events are sent out:
*
* - A number of #DC_EVENT_IMEX_PROGRESS events are sent and may be used to create
* a progress bar or stuff like that. Moreover, you'll be informed when the imex-job is done.
*
* - For each file written on export, the function sends #DC_EVENT_IMEX_FILE_WRITTEN
*
* Only one import-/export-progress can run at the same time.
* To cancel an import-/export-progress, use dc_stop_ongoing_process().
*
* @memberof dc_context_t
* @param context The context as created by dc_context_new().
* @param what One of the DC_IMEX_* constants.
* @param param1 Meaning depends on the DC_IMEX_* constants. If this parameter is a directory, it should not end with
* a slash (otherwise you'll get double slashes when receiving #DC_EVENT_IMEX_FILE_WRITTEN). Set to NULL if not used.
* @param param2 Meaning depends on the DC_IMEX_* constants. Set to NULL if not used.
* @return None.
*/
void dc_imex(dc_context_t* context, int what, const char* param1, const char* param2)
{
dc_param_t* param = dc_param_new();
dc_param_set_int(param, DC_PARAM_CMD, what);
dc_param_set (param, DC_PARAM_CMD_ARG, param1);
dc_param_set (param, DC_PARAM_CMD_ARG2, param2);
dc_job_kill_action(context, DC_JOB_IMEX_IMAP);
dc_job_add(context, DC_JOB_IMEX_IMAP, 0, param->packed, 0); // results in a call to dc_job_do_DC_JOB_IMEX_IMAP()
dc_param_unref(param);
}
void dc_job_do_DC_JOB_IMEX_IMAP(dc_context_t* context, dc_job_t* job)
{
int success = 0;
int ongoing_allocated_here = 0;
int what = 0;
char* param1 = NULL;
char* param2 = NULL;
if (context==NULL || context->magic!=DC_CONTEXT_MAGIC || context->sql==NULL) {
goto cleanup;
}
if (!dc_alloc_ongoing(context)) {
goto cleanup;
}
ongoing_allocated_here = 1;
what = dc_param_get_int(job->param, DC_PARAM_CMD, 0);
param1 = dc_param_get (job->param, DC_PARAM_CMD_ARG, NULL);
param2 = dc_param_get (job->param, DC_PARAM_CMD_ARG2, NULL);
if (param1==NULL) {
dc_log_error(context, 0, "No Import/export dir/file given.");
goto cleanup;
}
dc_log_info(context, 0, "Import/export process started.");
context->cb(context, DC_EVENT_IMEX_PROGRESS, 10, 0);
if (!dc_sqlite3_is_open(context->sql)) {
dc_log_error(context, 0, "Import/export: Database not opened.");
goto cleanup;
}
if (what==DC_IMEX_EXPORT_SELF_KEYS || what==DC_IMEX_EXPORT_BACKUP) {