/*
 *  Login.app
 * 
 *  Copyright (c) 1997-2000 Per Liden
 * 
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 
 *  USA.
 */

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include "preferences.h"
#include "util.h"
#include "resources.h"
#include "graphics.h"
#include "config.h"


static RImage* loadImage(const char* appPath, const char* theme, const char* file) {    
   RImage* image;
   char*   themeDir;
   char*   imageFile;

   themeDir = THEME_DIR;
   imageFile = alloc(strlen(appPath) + strlen(themeDir) + strlen(theme) + strlen(file) + 3);
   sprintf(imageFile, "%s%s/%s/%s", appPath, themeDir, theme, file);

   image = RLoadImage(ctx, imageFile, 0);

   if (!image) {
      printError("Could not load image %s (%s)", imageFile, RMessageForError(RErrorCode));
   }

   free(imageFile);

   return image;
}

Resources* allocResources(Display* dpy, const char* appPath, Preferences* preferences) {
   Resources* resources;

   resources = alloc(sizeof(Resources));

   /* Load images */
   resources->panelImage = loadImage(appPath, preferences->theme, preferences->panelImage);
   resources->fieldLabelsImage = loadImage(appPath, preferences->theme, preferences->fieldLabelsImage);
   resources->powerButtonUpImage = loadImage(appPath, preferences->theme, preferences->powerButtonUpImage);
   resources->powerButtonDownImage = loadImage(appPath, preferences->theme, preferences->powerButtonDownImage);
   resources->restartButtonUpImage = loadImage(appPath, preferences->theme, preferences->restartButtonUpImage);
   resources->restartButtonDownImage = loadImage(appPath, preferences->theme, preferences->restartButtonDownImage);
   resources->alertIcon = loadImage(appPath, preferences->theme, preferences->alertIcon);
   resources->inputUserImage = loadImage(appPath, preferences->theme, preferences->inputUserImage);
   resources->inputPasswordImage = loadImage(appPath, preferences->theme, preferences->inputPasswordImage);

   if (!resources->panelImage || !resources->fieldLabelsImage || 
       !resources->powerButtonUpImage || !resources->powerButtonDownImage || 
       !resources->restartButtonUpImage || !resources->restartButtonDownImage ||
       !resources->alertIcon || !resources->inputUserImage || !resources->inputPasswordImage) {
      return 0;
   }
	
   /* Allocate colors */
   if (strcasecmp(preferences->backgroundColor, "None")) {
      resources->backgroundColor = getColorByName(preferences->backgroundColor);
   }
   resources->messageColor = getColorByName(preferences->messageColor);
   resources->inputFontColor = getColorByName(preferences->inputFontColor);
   if (preferences->disableHostname == NO) {
      resources->hostnameColor = getColorByName(preferences->hostnameColor);
   }

   /* Load fonts */
   if ((resources->messageFont = XLoadQueryFont(dpy, preferences->messageFont)) == NULL) {
      printError("could not load message font: %s\n%s%s%s%s%s", preferences->messageFont, \
               "Try to edit your configuration file: ", appPath, "Themes/", preferences->theme, "/Settings");
      return 0;
   }
   if ((resources->inputFont = XLoadQueryFont(dpy, preferences->inputFont)) == NULL) {
      printError("could not load input font: %s\n%s%s%s%s%s", preferences->inputFont, \
               "Try to edit your configuration file: ", appPath, "Themes/", preferences->theme, "/Settings");
      return 0;
   }
   if (preferences->disableHostname == NO) {
      if ((resources->hostnameFont = XLoadQueryFont(dpy, preferences->hostnameFont)) == NULL) {
         printError("could not load hostname font: %s\n%s%s%s%s%s", preferences->hostnameFont, \
                  "Try to edit your configuration file: ", appPath, "Themes/", preferences->theme, "/Settings");
         return 0;
      }
   }
    
   /* Get hostname */
   if (preferences->disableHostname == NO) {
      resources->hostname = alloc(256);
      if (gethostname(resources->hostname, 255) != 0) {
         preferences->disableHostname = YES;
         printError("could not get hostname");
      }
   }

   return resources;
}
