[Frugalware-darcs] setup: configsource.c

voroskoi voroskoi at frugalware.org
Wed Jun 6 19:21:22 CEST 2007


Darcsweb-Url: http://darcs.frugalware.org/darcsweb/darcsweb.cgi?r=setup;a=darcs_commitdiff;h=20070606171137-dd049-f6331039310c14703e16542163f0f4955feec869.gz;

[configsource.c
voroskoi <voroskoi at frugalware.org>**20070606171137
 added a fw_checklist() before fw_inputbox()
 in mirrorselect part, so you can simply check preferred mirrors
] {
hunk ./src/plugins/configsource.c 60
-	=

+
hunk ./src/plugins/configsource.c 82
-int updateconfig(char *fn, char *mirror)
+GList *getmirrors(char *fn)
hunk ./src/plugins/configsource.c 84
-	FILE *fp, *tfp;
-	char line[PATH_MAX];
-	char *tfn;
-	int firstline=3D1;
-	=

-	tfn =3D strdup("/tmp/setup_XXXXXX");
-	mkstemp(tfn);
-	=

-	copyfile(fn, tfn);
-	if ((tfp =3D fopen(tfn, "r"))=3D=3D NULL)
-	{
-		perror(_("Could not open output file for writing"));
-		return(1);
+	FILE *fp;
+	char line[PATH_MAX], *ptr, *country;
+	GList *mirrors=3DNULL;
+
+	if ((fp =3D fopen(fn, "r"))=3D=3D NULL) { //fopen error
+		printf("Could not open output file for reading");
+		return(NULL);
+	}
+
+	while(!feof(fp)) {
+		if(fgets(line, PATH_MAX, fp) =3D=3D NULL)
+			break;
+		if(line =3D=3D strstr(line, "# - ")) { // country
+			ptr =3D strrchr(line, ' ');
+			*ptr =3D '\0';
+			ptr =3D strrchr(line, ' ')+1;
+			country =3D strdup(ptr);
+		}
+		if(line =3D=3D strstr(line, "Server =3D ")) { // server
+			ptr =3D strrchr(line, '/'); // drops frugalware-ARCH
+			*ptr =3D '\0';
+			ptr =3D strstr(line, "Server =3D ")+9; //drops 'Server =3D ' part
+			mirrors =3D g_list_append(mirrors, strdup(ptr));
+			mirrors =3D g_list_append(mirrors, strdup(country));
+			mirrors =3D g_list_append(mirrors, strdup("Off")); //unchecked by defau=
lt in checkbox
+		}
hunk ./src/plugins/configsource.c 111
+	fclose(fp);
+	return (mirrors);
+}
+
+int updateconfig(char *fn, GList *mirrors)
+{
+	FILE *fp;
+	short i;
+
hunk ./src/plugins/configsource.c 125
-	while(!feof(tfp))
-	{
-		if(fgets(line, 256, tfp) =3D=3D NULL)
-			break;
-		if((line =3D=3D strstr(line, "Server =3D ")) && firstline)
-		{
-			fprintf(fp, "Server =3D %s/frugalware-%s\n", mirror, ARCH);
-			fprintf(fp, line);
-			firstline=3D0;
-		}
-		else
-			fprintf(fp, line);
+	fprintf(fp, "#\n# %s repository\n#\n\n[%s]\n\n", PACCONF, PACCONF);
+	for (i=3D0; i<g_list_length(mirrors); i+=3D2) {
+		// do not change the country style as it will cause getmirrors() misbeha=
viour
+		fprintf(fp, "# - %s -\n", (char *)g_list_nth_data(mirrors, i+1));
+		fprintf(fp, "Server =3D %s/frugalware-%s\n", (char *)g_list_nth_data(mir=
rors, i), ARCH);
hunk ./src/plugins/configsource.c 131
-	fclose(tfp);
hunk ./src/plugins/configsource.c 132
-	unlink(tfn);
+	g_list_free(mirrors);
hunk ./src/plugins/configsource.c 136
-int mirrorconf(void)
+GList *mirrorconf(void)
hunk ./src/plugins/configsource.c 138
+	short i,j;
+	GList *mirrorlist=3DNULL, *newmirrorlist=3DNULL;
hunk ./src/plugins/configsource.c 141
+	char **arraychk;
+
hunk ./src/plugins/configsource.c 144
-	=

-	mirror =3D firstmirror(fn);
+
hunk ./src/plugins/configsource.c 148
-	if(fw_inputbox(_("Please select a mirror"), _("You may now specify "
-					"the mirror closest to you in order to "
-					"download the packages faster. In most "
-					"cases the default value will be "
-					"fine."), 0, 0, mirror, 0) =3D=3D -1)
-		return(-1);
hunk ./src/plugins/configsource.c 149
-	updateconfig(fn, dialog_vars.input_result);
-	=

-	return(0);
+	mirrorlist =3D getmirrors(fn);
+	arraychk =3D glist2dialog(mirrorlist);
+
+	newmirrorlist =3D fw_checklist(_("Please select mirrors"),
+					_("Here you can chose one or more "
+					"nearby mirror to speed up "
+					"package dowloading."),
+					0, 0, 0,
+					g_list_length(mirrorlist)/3,
+					arraychk,
+					FLAG_CHECK);
+
+	// removes the checkbox related part (Off state)
+	for (i=3D0; i<g_list_length(mirrorlist); i++) {
+		if (!strcmp(g_list_nth_data(mirrorlist, i), "Off")) {
+			mirrorlist =3D g_list_remove(mirrorlist, g_list_nth_data(mirrorlist, i)=
);
+		}
+	}
+	// adds country info to the selected mirrors
+	// also removes the duplicate mirrors
+	for (i=3D0; i<g_list_length(mirrorlist); i+=3D2) {
+		for (j=3D0; j<g_list_length(newmirrorlist); j++) {
+			if (!strcmp((char*)g_list_nth_data(mirrorlist, i), (char*)g_list_nth_da=
ta(newmirrorlist, j))) {
+				newmirrorlist =3D g_list_insert(newmirrorlist, g_list_nth_data(mirrorl=
ist, i+1), j+1);
+				mirrorlist =3D g_list_remove(mirrorlist, g_list_nth_data(mirrorlist, i=
));
+				mirrorlist =3D g_list_remove(mirrorlist, g_list_nth_data(mirrorlist, i=
));
+			}
+		}
+	}
+	// merges the selected and remain mirrors
+	newmirrorlist =3D g_list_concat(newmirrorlist, mirrorlist);
+	mirror =3D firstmirror(fn);
+	if(fw_inputbox(_("Custom mirror"), _("You may now specify "
+					"a custom mirror (eg. LAN) "
+					"so you can download packages "
+					"faster. In most cases a "
+					"Cancel enough here."), 0, 0, mirror, 0) !=3D -1) { //not cancel
+		if (strcmp(dialog_vars.input_result, "\0")) { //not empty
+				newmirrorlist =3D g_list_insert(newmirrorlist, strdup(dialog_vars.inpu=
t_result), 0);
+				newmirrorlist =3D g_list_insert(newmirrorlist, strdup("CUSTOM"), 1);
+		}
+	}
+	updateconfig(fn, newmirrorlist);
+	return(newmirrorlist);
hunk ./src/plugins/configsource.c 211
-		if(mirrorconf() =3D=3D -1)
+		if(mirrorconf() =3D=3D NULL)
}


More information about the Frugalware-darcs mailing list