Atril (evince, document viewer) default directory for Save As

For the XFCE desktop environment Atril is the default PDF viewer and I also use it to open any PDF files from Firefox. It is a great lightweight document viewer which does what I need most of the time. One exception is when after opening a PDF file from Firefox I decide to keep the file and select File Save As. Atril insists on saving the file in $HOME/Documents, which is practically never the directory I want to use…

The only partial workaround I found suggested that I change the XDG user document directory, for example

xdg-user-dirs-update --set DOCUMENTS $HOME/Downloads/PDFs

After the above change Atril indeed will use $HOME/Downloads/PDFs as its default directory, but so will all other office applications, which again is not what I want. The only option left was to modify the Atril source code and to recompile the application.

I decided to add a new commend line option, –documents-dir or -d, which allows the user to specify a custom documents directory. If no documents directory is specified on the command line, the old behaviour is maintained, i.e. XDG_DOCUMENTS_DIR is used if it is defined and valid, or as a fallback, the user’s home directory is used.

Enable source repositories

sudo nano /etc/apt/sources.list
grep deb-src /etc/apt/sources.list
deb-src http://gb.archive.ubuntu.com/ubuntu/ focal multiverse universe main restricted #Added by software-properties deb-src http://gb.archive.ubuntu.com/ubuntu/ focal-updates multiverse universe main restricted #Added by software-properties # deb-src http://gb.archive.ubuntu.com/ubuntu/ focal universe # deb-src http://gb.archive.ubuntu.com/ubuntu/ focal-updates universe # deb-src http://gb.archive.ubuntu.com/ubuntu/ focal multiverse # deb-src http://gb.archive.ubuntu.com/ubuntu/ focal-updates multiverse deb-src http://gb.archive.ubuntu.com/ubuntu/ focal-backports main restricted universe multiverse #Added by software-properties # deb-src http://archive.canonical.com/ubuntu focal partner deb-src http://security.ubuntu.com/ubuntu focal-security multiverse universe main restricted #Added by software-properties # deb-src http://security.ubuntu.com/ubuntu focal-security universe # deb-src http://security.ubuntu.com/ubuntu focal-security multiverse

Install dependencies (some of these you may already have on your system, some may not actually be needed, and some that are needed I may have missed and are not listed below because I already had them on my system)

sudo apt install build-essential devscripts
sudo apt install mate-common yelp-tools autopoint libglib2.0-dev libgtk-3-dev libgail-3-dev libxml2-dev libsecret-1-dev libcaja-extension-dev libsynctex-dev libpoppler-glib-dev libdjvulibre-dev libspectre-dev libgxps-dev libkpathsea-dev libepub-dev

Create a working directory, download the package source code and install any additional dependencies

mkdir -p deb/atril
cd debs/atril
apt-get source atril
sudo apt-get build-dep atril

Modify the package descriptor

debchange -i
cd atril-1.24.0-1
for f in shell/main.c shell/ev-window.c shell/ev-window.h ; do cp -i $f $f.ori ; done

Modify the source

nano shell/main.c
nano shell/ev-window.c
nano shell/ev-window.h
for f in shell/main.c shell/ev-window.c shell/ev-window.h ; do diff -u $f.ori $f ; done
--- shell/main.c.ori	2020-02-09 22:41:16.000000000 +0000
+++ shell/main.c	2021-04-05 20:20:12.175627857 +0100
@@ -32,6 +32,7 @@
 #include "ev-file-helpers.h"
 #include "ev-stock-icons.h"
 #include "ev-metadata.h"
+#include "ev-window.h"
 
 #include "eggsmclient.h"
 #include "eggdesktopfile.h"
@@ -63,6 +64,7 @@
 
 static const GOptionEntry goption_options[] =
 {
+ 	{ "documents-dir", 'd', 0, G_OPTION_ARG_STRING, &ev_documents_dir, N_("Default documents directory."), N_("DIRECTORY")},
 	{ "page-label", 'p', 0, G_OPTION_ARG_STRING, &ev_page_label, N_("The page label of the document to display."), N_("PAGE")},
 	{ "page-index", 'i', 0, G_OPTION_ARG_INT, &ev_page_index, N_("The page number of the document to display."), N_("NUMBER")},
 	{ "fullscreen", 'f', 0, G_OPTION_ARG_NONE, &fullscreen_mode, N_("Run atril in fullscreen mode"), NULL },
--- shell/ev-window.c.ori	2020-02-09 22:41:16.000000000 +0000
+++ shell/ev-window.c	2021-04-05 20:24:50.204231441 +0100
@@ -3246,6 +3246,9 @@
 	gtk_widget_destroy (fc);
 }
 
+
+const gchar   *ev_documents_dir;
+
 static void
 ev_window_cmd_save_as (GtkAction *action, EvWindow *ev_window)
 {
@@ -3275,9 +3278,17 @@
 	gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (fc), base_name);
 	g_free (base_name);
 
-	documents_dir = g_get_user_special_dir (G_USER_DIRECTORY_DOCUMENTS);
-	default_dir = g_file_test (documents_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR) ?
-	              documents_dir : g_get_home_dir ();
+ 	// if documents directory is specified on the command line and is valid, use it
+ 	// else check XDG user documents directory
+ 	// otherwise default to home directory
+ 	if ( g_file_test (ev_documents_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR) ) {
+ 		default_dir = ev_documents_dir;
+ 	}
+ 	else {
+ 		documents_dir = g_get_user_special_dir (G_USER_DIRECTORY_DOCUMENTS);
+ 		default_dir = g_file_test (documents_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR) ?
+ 				              documents_dir : g_get_home_dir ();
+ 	}
 
 	tmp_dir = g_build_filename ("tmp", NULL);
 	var_tmp_dir = g_build_filename ("var", "tmp", NULL);
--- shell/ev-window.h.ori	2020-02-09 22:41:16.000000000 +0000
+++ shell/ev-window.h	2021-04-05 20:16:37.302206465 +0100
@@ -30,6 +30,8 @@
 
 G_BEGIN_DECLS
 
+extern const gchar   *ev_documents_dir;
+
 typedef enum {
 	EV_WINDOW_MODE_NORMAL,
 	EV_WINDOW_MODE_FULLSCREEN,
@@ -87,7 +89,6 @@
 					 int		 last_page);
 const gchar *	ev_window_get_dbus_object_path (EvWindow *ev_window);
 
-
 G_END_DECLS
 
 #endif /* !EV_WINDOW_H */

Compile the modified source

dpkg-buildpackage -us -uc -nc

If all went well there should have been a summary of the Atril configuration that looked something like this

Configure summary: GTK+ Unix Print…..: yes Keyring Support…..: yes DBUS Support……..: yes Caja Plugin………: yes Thumbnailer………: yes Previewer………..: yes Gtk-Doc Support…..: yes Debug mode……….: no GObj. Introspection.: yes Tests……………: yes PDF Backend………: yes PostScript Backend..: yes TIFF Backend……..: yes DJVU Backend……..: yes DVI Backend………: yes Pixbuf Backend……: yes Comics Backend……: yes XPS Backend………: yes ePub Backend……..: yes SyncTeX………….: yes

and a number of .deb packages should have been created by the end of the compuilation

cd ..
ls -l  *.deb
-rw-r–r– 1 laczik laczik 184748 Apr 5 20:26 atril_1.24.0-1zjl1_amd64.deb -rw-r–r– 1 laczik laczik 909712 Apr 5 20:26 atril-common_1.24.0-1zjl1_all.deb -rw-r–r– 1 laczik laczik 11464 Apr 5 20:26 gir1.2-atril_1.24.0-1zjl1_amd64.deb -rw-r–r– 1 laczik laczik 24496 Apr 5 20:26 gir1.2-atrildocument-1.5.0_1.24.0-1zjl1_amd64.deb -rw-r–r– 1 laczik laczik 18004 Apr 5 20:26 gir1.2-atrilview-1.5.0_1.24.0-1zjl1_amd64.deb -rw-r–r– 1 laczik laczik 182268 Apr 5 20:26 libatrildocument3_1.24.0-1zjl1_amd64.deb -rw-r–r– 1 laczik laczik 50520 Apr 5 20:26 libatrildocument-dev_1.24.0-1zjl1_amd64.deb -rw-r–r– 1 laczik laczik 120708 Apr 5 20:26 libatrilview3_1.24.0-1zjl1_amd64.deb -rw-r–r– 1 laczik laczik 27088 Apr 5 20:26 libatrilview-dev_1.24.0-1zjl1_amd64.deb

You can now install the shiny new packages you have just created

apt list installed '*atril*'
sudo dpkg -r atril atril-common gir1.2-atril gir1.2-sudo dpkg -r atril atril-common gir1.2-atril gir1.2-atrildocument-1.5.0 gir1.2-atrilview-1.5.0 libatrildocument-dev libatrildocument3 libatrilview-dev libatrilview3
sudo dpkg -i ./*.deb

Alternatively, if you are running Xubuntu or Ubuntu 20.04 LTS, you can just simply use the pre-compiled packages from my PPA

sudo add-apt-repository ppa:laczik/ppa
sudo apt-get update
sudo apt upgrade atril

Once the new modified version of Atril was installed I created a script that is now called from Firefox to achieve the behaviour I want

nano $HOME/bin/atril_with_def_dir.sh
chmod +x $HOME/bin/atril_with_def_dir.sh
cat $HOME/bin/atril_with_def_dir.sh
#!/bin/bash # atril –documents-dir=”$HOME/Downloads/PDFs” $*

Leave a Reply

Your email address will not be published. Required fields are marked *