1 // ---------------------------------------------------------------------
2 //
3 //      logbook.cxx
4 //
5 // Copyright (C) 2008-2012
6 //               Dave Freese, W1HKJ
7 //
8 // This library is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with the program; if not, write to the
20 //
21 //  Free Software Foundation, Inc.
22 //  51 Franklin Street, Fifth Floor
23 //  Boston, MA  02110-1301 USA.
24 //
25 // ---------------------------------------------------------------------
26 
27 #include <config.h>
28 #include <iostream>
29 #include <fstream>
30 #include <sstream>
31 #include <cstring>
32 
33 #include <FL/Fl.H>
34 #include <FL/filename.H>
35 
36 #include "fllog.h"
37 #include "logbook.h"
38 #include "debug.h"
39 #include "status.h"
40 
41 using namespace std;
42 
43 std::string log_checksum;
44 
rotate_log(std::string filename)45 void rotate_log(std::string filename)
46 {
47 	FILE *fp;
48 	std::string oldfn, newfn;
49 	const char *ext[] = {".5", ".4", ".3", ".2", ".1"};
50 
51 	for (int i = 0; i < 4; i++) {
52 		newfn.assign(filename).append(ext[i]);
53 		oldfn.assign(filename).append(ext[i+1]);
54 		if ((fp = fopen(oldfn.c_str(), "r")) == NULL)
55 			continue;
56 		fclose(fp);
57 		rename(oldfn.c_str(), newfn.c_str());
58 	}
59 
60 	newfn.assign(filename).append(ext[4]);
61 	char buffer[65536];
62 
63 	FILE *original = fopen(filename.c_str(), "rb");
64 	if (original) {
65 		FILE *backup = fopen(newfn.c_str(), "wb");
66 		if (backup) {
67 			size_t n;
68 			while (1) {
69 				memset(buffer, 0, sizeof(buffer));
70 				n = fread(buffer, 1, sizeof(buffer), original);
71 				n = fwrite(buffer, 1, n, backup);
72 				if (feof(original)) {
73 					break;
74 				}
75 			}
76 			fflush(backup);
77 			fclose(backup);
78 		}
79 		fclose(original);
80 	}
81 }
82 
do_load_browser(void *)83 void do_load_browser(void *)
84 {
85 	loadBrowser();
86 }
87 
start(void *)88 void start(void *)
89 {
90 	if (progStatus.logbookfilename.empty()) {
91 		logbook_filename = LogHomeDir;
92 		logbook_filename.append("logbook." ADIF_SUFFIX);
93 		progStatus.logbookfilename = logbook_filename;
94 	} else
95 		logbook_filename = progStatus.logbookfilename;
96 
97 	qsodb.deleteRecs();
98 	adifFile.readFile (logbook_filename.c_str(), &qsodb);
99 
100 	rotate_log(logbook_filename);
101 
102 	txtLogFile->value(progStatus.logbookfilename.c_str());
103 	txtLogFile->redraw();
104 
105 	dlgLogbook->copy_label("Logbook Server");
106 
107 	restore_sort();
108 	loadBrowser();
109 
110 	qsodb.isdirty(0);
111 	activateButtons();
112 
113 }
114 
start_logbook()115 void start_logbook ()
116 {
117 	Fl::add_timeout(0.2, start);
118 }
119 
close_logbook()120 void close_logbook()
121 {
122 	saveLogbook();
123 }
124 
125