1#include <Alert.h>
2#include <stdio.h>
3#include <File.h>
4#include "BackupWindow.h"
5#include "NetworkWindow.h"
6#include "NetListView.h"
7#include "ConfigData.h"
8
9BackupWin::BackupWin(const BRect &frame)
10 :	BWindow(frame,"Backup Win",B_MODAL_WINDOW,
11			B_NOT_ANCHORED_ON_ACTIVATE | B_NOT_RESIZABLE,B_CURRENT_WORKSPACE)
12{
13	BRect r(Bounds());
14
15	BView *view=new BView(Bounds(),"Backup",B_FOLLOW_ALL_SIDES,B_WILL_DRAW);
16	view->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
17	AddChild(view);
18
19	fDone=new BButton(BRect(0,0,1,1),"Done","Done",new BMessage(kSaveBackup_M));
20	fDone->ResizeToPreferred();
21	fDone->MoveTo(r.right - 5 - fDone->Bounds().Width(),
22					r.bottom - 5 - fDone->Bounds().Height());
23
24	fCancel=new BButton(BRect(100,50,190,70),"Cancel","Cancel",
25							new BMessage(kCancel_bak_M));
26	fCancel->ResizeToPreferred();
27	fCancel->MoveTo(fDone->Frame().left - 5 - fCancel->Frame().Width(),
28				fDone->Frame().top);
29
30	fName=new BTextControl(BRect(5,5,r.right - 10,40),"Backup As","Backup As:",
31								NULL,new BMessage(),B_FOLLOW_LEFT_RIGHT | B_FOLLOW_TOP);
32	float w,h;
33	fName->GetPreferredSize(&w,&h);
34	fName->ResizeTo(r.right - 10, h);
35	fName->MoveTo(5, 5 + (fCancel->Frame().top-h-10)/2);
36	fName->SetDivider(fName->StringWidth("Backup As:")+10);
37
38	view->AddChild(fName);
39	view->AddChild(fCancel);
40	view->AddChild(fDone);
41	fDone->MakeDefault(true);
42	fName->MakeFocus(true);
43}
44
45void
46BackupWin::MessageReceived(BMessage *message)
47{
48	switch (message->what) {
49
50		case kCancel_bak_M: {
51			PostMessage(B_QUIT_REQUESTED);
52			break;
53		}
54		case kSaveBackup_M: {
55
56			if (strlen(fName->Text()) > 0) {
57				SaveBackup(fName->Text());     //Writes the new config file
58				fParentWindow->Lock();
59				fParentWindow->LoadConfigEntries(); //Update the list
60				fParentWindow->fConfigurationsList->SelectionChanged(); //Update the buttons
61				fParentWindow->Unlock();
62				Quit();
63			}
64			else {
65				BAlert *alert = new BAlert("Backup Info Alert","You must specify a name.","OK");
66				alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
67				alert->Go();
68			}
69			break;
70		}
71	}
72}
73
74void
75BackupWin::SaveBackup(const char *bak_name)
76{
77/*
78	// GLOBAL: section
79	BString DNS_DOMAIN  	 = "\n\tDNS_DOMAIN = ";
80			DNS_DOMAIN       << fParentWindow->fDomain->Text();
81	BString HOSTNAME    	 = "\n\tHOSTNAME = ";
82			HOSTNAME         << fParentWindow->fHost->Text();
83	BString USERNAME  	     = "\n\tUSERNAME = ";
84			USERNAME		 << fParentWindow->flogin_info_S[0];
85	BString PROTOCOLS 	     = "\n\tPROTOCOLS = ";//where to get that info ??
86	BString PASSWORD		 = "\n\tPASSWORD = ";
87			PASSWORD         << fParentWindow->flogin_info_S[1];
88	BString FTP_ENABLED		 = "\n\tFTP_ENABLED = ";
89		if (fParentWindow->fFTPServer->Value() == 0)
90			     FTP_ENABLED << 0;
91		else     FTP_ENABLED << 1;
92	BString IP_FORWARD 		 = "\n\tIP_FORWARD = ";
93		if (fParentWindow->fIPForwarding->Value() == 0)
94			      IP_FORWARD << 0;
95		else      IP_FORWARD << 1;
96	BString DNS_ENABLED	     = "\n\tDNS_ENABLED = ";//where to get that info ??
97	BString TELNETD_ENABLED  = "\n\tTELNETD_ENABLED = ";
98		if (fParentWindow->fTelnetServer->Value() == 0)
99			 TELNETD_ENABLED << 0;
100		else TELNETD_ENABLED << 1;
101	BString DNS_PRIMARY 	 = "\n\tDNS_PRIMARY = ";
102			DNS_PRIMARY		 << fParentWindow->fPrimaryDNS->Text();
103	BString DNS_SECONDARY	 = "\n\tDNS_SECONDARY = ";
104			DNS_SECONDARY	 << fParentWindow->fSecondaryDNS->Text();
105	BString ROUTER		     = "\n\tROUTER = ";//where to get that info ??
106	BString VERSION		     = "\n\tVERSION = ";//where to get that info ??
107	BString INTERFACES	     = "\n\tINTERFACES = ";//Unused but kept for background compatibility
108
109	// Now let's see how many interfaces there are
110	int32 numb = fParentWindow->fInterfacesList->CountItems();
111
112	// Each interface has 8 fields
113	BString fields[numb][8];
114
115	for(int i=0;i<numb;i++){ //Let's fill the fields
116		fields[i][0] = "\n\tDEVICECONFIG = "; //where to get that info ??
117		fields[i][1] = "\n\tDEVICELINK = "; //where to get that info ??
118		fields[i][2] = "\n\tDEVICETYPE = "; //where to get that info ??
119		fields[i][3] = "\n\tIPADDRESS = ";
120		fields[i][4] = "\n\tNETMASK = ";
121		fields[i][5] = "\n\tPRETTYNAME = ";
122		fields[i][6] = "\n\tENABLED = ";
123		fields[i][7] = "\n\tDHCP = ";
124	}
125
126	for(int i=0;i<numb;i++){ //Now let's put the data
127		NetListItem *item = dynamic_cast <NetListItem *> (fParentWindow->fInterfacesList->ItemAt(i));
128
129		fields[i][3] << item->fIPADDRESS;
130		fields[i][4] << item->fNETMASK;
131		fields[i][5] << item->fPRETTYNAME;
132		fields[i][6] << item->fENABLED;
133		fields[i][7] << item->fDHCP;
134	}
135
136	// Now let's write all this stuff
137	BString path;
138	path = "/boot/home/config/settings/network";
139
140	// This SaveBackup() function is used for backups(so a network.<name> file)
141	// and for the "Save" button which uses the network file as the backup file.
142	if (bak_name != NULL)
143		path << "." << bak_name;
144	else {
145		BEntry entry(path.String());
146		entry.Remove();
147	}
148
149	FILE *bak_file = fopen(path.String(),"w");
150	fprintf(bak_file,"GLOBAL:");		//The "GLOBAL" part
151	fprintf(bak_file,DNS_DOMAIN.String());
152	fprintf(bak_file,HOSTNAME.String());
153	fprintf(bak_file,USERNAME.String());
154	fprintf(bak_file,PROTOCOLS.String());
155	fprintf(bak_file,PASSWORD.String());
156	fprintf(bak_file,FTP_ENABLED.String());
157	fprintf(bak_file,IP_FORWARD.String());
158	fprintf(bak_file,DNS_ENABLED.String());
159	fprintf(bak_file,TELNETD_ENABLED.String());
160	fprintf(bak_file,DNS_PRIMARY.String());
161	fprintf(bak_file,DNS_SECONDARY.String());
162	fprintf(bak_file,ROUTER.String());
163	fprintf(bak_file,VERSION.String());
164	fprintf(bak_file,INTERFACES.String());
165
166	for (int i=0;i<numb;i++) {
167
168		// The interfacexx parts
169		BString interfacex = "\ninterface";
170		interfacex << i << ":";
171		fprintf(bak_file,interfacex.String());
172		for (int j=0;j<8;j++)
173			fprintf(bak_file,fields[i][j].String());
174	}
175
176	fclose(bak_file);
177*/
178}
179