1 /*
2  * PhotoPrint
3  * Copyright (c) 2004-2009 by Alastair M. Robinson
4  * Distributed under the terms of the GNU General Public License -
5  * see the file named "COPYING" for more details.
6  *
7  */
8 
9 #include <iostream>
10 
11 #include <getopt.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <locale.h>
15 #include <gtk/gtk.h>
16 #include <gutenprint/gutenprint.h>
17 
18 #include "config.h"
19 #include "gettext.h"
20 
21 #include "support/debug.h"
22 #include "support/configdb.h"
23 #include "photoprint_state.h"
24 
25 #include "pp_mainwindow.h"
26 #include "progressbar.h"
27 #include "support/progresstext.h"
28 #include "dialogs.h"
29 #include "miscwidgets/generaldialogs.h"
30 #include "splashscreen/splashscreen.h"
31 #include "profilemanager/profileselector.h"
32 #include "profilemanager/intentselector.h"
33 #include "miscwidgets/patheditor.h"
34 
35 #include "support/pathsupport.h"
36 #include "support/util.h"
37 
38 
39 #define _(x) gettext(x)
40 #define N_(x) gettext_noop(x)
41 
42 using namespace std;
43 
ParseOptions(int argc,char * argv[],char ** presetname)44 bool ParseOptions(int argc,char *argv[],char **presetname)
45 {
46 	int batchmode=false;
47 	static struct option long_options[] =
48 	{
49 		{"help",no_argument,NULL,'h'},
50 		{"version",no_argument,NULL,'v'},
51 		{"preset",required_argument,NULL,'p'},
52 		{"batch",no_argument,NULL,'b'},
53 		{"debug",required_argument,NULL,'d'},
54 		{0, 0, 0, 0}
55 	};
56 
57 	while(1)
58 	{
59 		int c;
60 		c = getopt_long(argc,argv,"hvp:bd:",long_options,NULL);
61 		if(c==-1)
62 			break;
63 		switch (c)
64 		{
65 			case 'h':
66 				printf("Usage: %s [options] image1 [image2] ... \n",argv[0]);
67 				printf("\t -h --help\t\tdisplay this message\n");
68 				printf("\t -v --version\t\tdisplay version\n");
69 				printf("\t -p --preset\t\tread a specific preset file\n");
70 				printf("\t -b --batch\t\trun without user interface\n");
71 				printf("\t -d --debug\t\tset debugging level - 0 for silent, 4 for verbose");
72 				throw 0;
73 				break;
74 			case 'v':
75 				printf("%s\n",PACKAGE_STRING);
76 				throw 0;
77 				break;
78 			case 'p':
79 				*presetname=optarg;
80 				break;
81 			case 'b':
82 				batchmode=true;
83 				break;
84 			case 'd':
85 				Debug.SetLevel(DebugLevel(atoi(optarg)));
86 				break;
87 		}
88 	}
89 	return(batchmode);
90 }
91 
92 
destroy(GtkWidget * widget,gpointer data)93 static void destroy( GtkWidget *widget,
94                      gpointer   data )
95 {
96     gtk_main_quit ();
97 }
98 
99 
main(int argc,char ** argv)100 int main(int argc,char **argv)
101 {
102 	Debug[TRACE] << "Photoprint starting..." << endl;
103 	gboolean have_gtk=false;
104 	char *presetname=NULL;
105 
106 	Debug.SetLevel(WARN);
107 #ifdef WIN32
108 	char *logname=substitute_homedir("$HOME" SEARCHPATH_SEPARATOR_S ".photoprint_errorlog");
109 	Debug.SetLogFile(logname);
110 	free(logname);
111 #endif
112 
113 	try
114 	{
115 		bool batchmode=ParseOptions(argc,argv,&presetname);
116 		if(!batchmode)
117 			have_gtk=gtk_init_check (&argc, &argv);
118 
119 		if(have_gtk)
120 			gtk_set_locale();
121 		else
122 		{
123 			g_type_init();
124 			setlocale(LC_ALL,"");
125 		}
126 
127 		bindtextdomain(PACKAGE,LOCALEDIR);
128 		bind_textdomain_codeset(PACKAGE, "UTF-8");
129 		textdomain(PACKAGE);
130 
131 		SplashScreen *splash=NULL;
132 		if(have_gtk)
133 		{
134 			splash=new SplashScreen;
135 			splash->SetMessage(_("Initializing..."));
136 		}
137 
138 		PhotoPrint_State state(batchmode);
139 
140 		if(presetname)
141 			state.SetFilename(presetname);
142 
143 		if(have_gtk)
144 			splash->SetMessage(_("Checking .photoprint directory..."));
145 
146 		CheckSettingsDir(".photoprint");
147 
148 		if(have_gtk)
149 			splash->SetMessage(_("Loading preset..."));
150 		state.ParseConfigFile();
151 
152 		if(have_gtk)
153 		{
154 			splash->SetMessage(_("Creating layout..."));
155 			delete splash;
156 		}
157 
158 		state.NewLayout();
159 
160 		if(batchmode)
161 		{
162 			try
163 			{
164 				Debug[TRACE] << "Running in batch mode" << endl;
165 				if(argc>optind)
166 				{
167 					bool allowcropping=state.layoutdb.FindInt("AllowCropping");
168 					enum PP_ROTATION rotation=PP_ROTATION(state.layoutdb.FindInt("Rotation"));
169 					for(int i=optind;i<argc;++i)
170 					{
171 						Debug[TRACE] << "Adding file: " << argv[i] << endl;
172 						state.layout->AddImage(argv[i],allowcropping,rotation);
173 					}
174 					ProgressText p;
175 					state.layout->Print(&p);
176 				}
177 			}
178 			catch(const char *err)
179 			{
180 				Debug[ERROR] << "Error: " << err << endl;
181 			}
182 		}
183 		else
184 		{
185 			try
186 			{
187 				GtkWidget *mainwindow;
188 				mainwindow = pp_mainwindow_new(&state);
189 				g_signal_connect (G_OBJECT (mainwindow), "destroy",
190 					    G_CALLBACK (destroy), NULL);
191 				gtk_widget_show (mainwindow);
192 
193 				if(argc>optind)
194 				{
195 					bool allowcropping=state.layoutdb.FindInt("AllowCropping");
196 					enum PP_ROTATION rotation=PP_ROTATION(state.layoutdb.FindInt("Rotation"));
197 					ProgressBar p(_("Loading images..."),true,mainwindow);
198 					int lastpage=0;
199 					for(int i=optind;i<argc;++i)
200 					{
201 						if(!p.DoProgress(i-optind,argc-optind))
202 							break;
203 						lastpage=state.layout->AddImage(argv[i],allowcropping,rotation);
204 					}
205 					state.layout->SetCurrentPage(lastpage);
206 				}
207 
208 				pp_mainwindow_refresh(PP_MAINWINDOW(mainwindow));
209 
210 				gtk_main ();
211 			}
212 			catch (const char *err)
213 			{
214 				ErrorMessage_Dialog(err);
215 			}
216 		}
217 	}
218 	catch(const char *err)
219 	{
220 		if(have_gtk)
221 			ErrorMessage_Dialog(err);
222 		Debug[ERROR] << "Error: " << err << endl;
223 	}
224 	catch(int retcode)
225 	{
226 		return(retcode);
227 	}
228 	return(0);
229 }
230