1 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 /// Name:         Main wxBase App
3 ///
4 /// Purpose:      aMule ed2k link creator
5 ///
6 /// Author:       ThePolish <thepolish@vipmail.ru>
7 ///
8 /// Copyright (c) 2004-2011 ThePolish ( thepolish@vipmail.ru )
9 ///
10 /// This program is free software; you can redistribute it and/or modify
11 /// it under the terms of the GNU General Public License as published by
12 /// the Free Software Foundation; either version 2 of the License, or
13 /// (at your option) any later version.
14 ///
15 /// This program is distributed in the hope that it will be useful,
16 /// but WITHOUT ANY WARRANTY; without even the implied warranty of
17 /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 /// GNU General Public License for more details.
19 ///
20 /// You should have received a copy of the GNU General Public License
21 /// along with this program; if not, write to the
22 /// Free Software Foundation, Inc.,
23 /// 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA
24 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
25 
26 
27 
28 #include "config.h"		// Needed for PACKAGE
29 
30 #include <wx/log.h>
31 
32 #include "alcc.h"
33 #include "ed2khash.h"
34 
35 // Application implementation
IMPLEMENT_APP(alcc)36 IMPLEMENT_APP (alcc)
37 
38 /// Running Alcc
39 int alcc::OnRun ()
40 {
41   // Used to tell alcc to use aMule catalog
42   m_locale.Init();
43   m_locale.AddCatalog(wxT(PACKAGE));
44 
45   wxLog::DontCreateOnDemand();
46   wxLogStderr * stderrLog = new wxLogStderr;
47   wxLogStderr * stdoutLog = new wxLogStderr(stdout);
48   delete wxLog::SetActiveTarget(stderrLog); // Log on Stderr
49 #if wxCHECK_VERSION(2, 9, 0)
50   wxLog::SetTimestamp("");   // Disable timestamp on messages
51 #else
52   wxLog::SetTimestamp(NULL); // Disable timestamp on messages
53 #endif
54 
55   Ed2kHash hash;
56   size_t i;
57   for (i=0;i<(m_filesToHash.GetCount());++i)
58     {
59       if (wxFileExists(m_filesToHash[i]))
60         {
61           if (m_flagVerbose)
62             {
63               wxLogMessage(_("Processing file number %u: %s"),i+1,m_filesToHash[i].c_str());
64 
65               if (m_flagPartHashes)
66                 {
67                   wxLogMessage(_("You have asked for part hashes (Only used for files > 9.5 MB)"));
68                 }
69             }
70 
71           if (hash.SetED2KHashFromFile(m_filesToHash[i], NULL))
72             {
73 				// Print the link to stdout
74 				wxLog::SetActiveTarget(stdoutLog);
75                 wxLogMessage(wxT("%s"), hash.GetED2KLink(m_flagPartHashes).c_str());
76 				// Everything else goes to stderr
77 				wxLog::SetActiveTarget(stderrLog);
78             }
79         }
80       else
81         {
82             if (m_flagVerbose)
83                 {
84                     wxLogMessage(_("%s ---> Non existant file !\n"),m_filesToHash[i].c_str());
85                 }
86         }
87     }
88   delete stdoutLog;
89   return 0;
90 }
91 
92 // On exit
93 int
OnExit()94 alcc::OnExit()
95 {
96   delete wxLog::SetActiveTarget(NULL);
97   return 0;
98 }
99 
100 /// Parse command line
OnInitCmdLine(wxCmdLineParser & cmdline)101 void alcc::OnInitCmdLine(wxCmdLineParser& cmdline)
102 {
103 	cmdline.AddSwitch(wxT("h"), wxT("help"), wxT("show this help message"), wxCMD_LINE_OPTION_HELP);
104 	cmdline.AddSwitch(wxT("v"), wxT("verbose"), wxT("be verbose"));
105 	cmdline.AddSwitch(wxT("p"), wxT("parthashes"), wxT("add part-hashes to ed2k link"));
106 	cmdline.AddParam(wxT("input files"), wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE);
107 }
108 
109 /// Command line preocessing
OnCmdLineParsed(wxCmdLineParser & cmdline)110 bool alcc::OnCmdLineParsed(wxCmdLineParser& cmdline)
111 {
112 
113   wxFileName filename;
114   size_t i;
115 
116   m_flagVerbose = cmdline.Found(wxT("v"));
117   m_flagPartHashes = cmdline.Found(wxT("p"));
118 
119   m_filesToHash.Clear();
120   for (i = 0; i < cmdline.GetParamCount(); ++i)
121     {
122       filename.Assign(cmdline.GetParam(i));
123       m_filesToHash.Add(filename.GetFullPath());
124     }
125   m_filesToHash.Shrink();
126 
127   return true;
128 }
129 // File_checked_for_headers
130