Exemplo n.º 1
0
unsigned char cache_preview ( pnd_disco_t *app, unsigned int maxwidth, unsigned int maxheight ) {
  SDL_Surface *s;
  mm_cache_t *c;

  // does this sucker even have a preview?
  if ( ! app -> preview_pic1 ) {
    return ( 1 ); // nothing here, so thats fine
  }

  // check if already cached
  if ( ( c = cache_query_preview ( app -> unique_id ) ) ) {
    return ( 1 ); // already got it
  }

  // not cached, load it up
  //

  // show hourglass
  ui_show_hourglass ( 1 /* updaterect*/ );

  // see if we can mount the pnd/dir
  // does preview file exist?
  //   if so, load it up, size it, cache it
  //   if not, warning and bail
  // unmount it

  // can we mount? or can we find it in preview cache? or an override?
  char fullpath [ PATH_MAX ] = "";
  char filepath [ PATH_MAX ] = "";

  // first, check for preview override
  {
    char ovrfile [ PATH_MAX ];
    char *fooby;
    sprintf ( ovrfile, "%s/%s", app -> object_path, app -> object_filename );
    fooby = strcasestr ( ovrfile, PND_PACKAGE_FILEEXT );
    if ( fooby ) {
      sprintf ( fooby, "_pvw#%u.png", app -> subapp_number );
      struct stat statbuf;
      if ( stat ( ovrfile, &statbuf ) == 0 ) {
	strncpy ( filepath, ovrfile, PATH_MAX );
      } // stat
    } // ovr?
  }

  // if not yet found, try to find in cache
  if ( filepath [ 0 ] == '\0' && g_pvwcache ) {
    static char *cache_findpath = NULL;
    if ( ! cache_findpath ) {
      cache_findpath = pnd_conf_get_as_char ( g_conf, "previewpic.cache_findpath" );
    }
    char buffer [ FILENAME_MAX ];
    sprintf ( buffer, "%s.png", app -> unique_id );
    char *f = pnd_locate_filename ( cache_findpath, buffer );
    if ( f ) {
      strncpy ( filepath, f, PATH_MAX );
    }
  }

  // unique-id to use for the cache mount
  char *uid = app -> unique_id;

  if ( app -> appdata_dirname ) {
    uid = app -> appdata_dirname;
  }

  // if we don't have a file path sorted out yet, means we need to mount and figure it
  if ( ! filepath [ 0 ] ) {
    sprintf ( fullpath, "%s/%s", app -> object_path, app -> object_filename );

    if ( ! pnd_pnd_mount ( pnd_run_script, fullpath, uid ) ) {
      pnd_log ( pndn_debug, "Couldn't mount '%s' for preview\n", fullpath );
      return ( 0 ); // couldn't mount?!
    }

    sprintf ( filepath, "%s/%s/%s", PND_MOUNT_PATH, uid, app -> preview_pic1 );
  }

  // load whatever path we've got
  s = IMG_Load ( filepath );

  if ( ! s ) {
    // unmount it, if mounted
    if ( fullpath [ 0 ] ) {
      pnd_pnd_unmount ( pnd_run_script, fullpath, uid );
    }
    pnd_log ( pndn_debug, "Couldn't open image '%s' for preview\n", filepath );
    return ( 0 );
  }

  // try to copy file to the cache, if we're doing that, and if mounted
  if ( g_pvwcache && fullpath [ 0 ] ) {
    char cacheoutpath [ PATH_MAX ] = "";

    // figure out where we want to write the file to
    if ( cache_find_writable ( app -> object_path, cacheoutpath, PATH_MAX ) ) {
      static char *cache_path = NULL;
      char buffer [ PATH_MAX ];
      if ( ! cache_path ) {
	cache_path = pnd_conf_get_as_char ( g_conf, "previewpic.cache_path" );
      }
      // make the dir
      snprintf ( buffer, PATH_MAX, "%s/%s", cacheoutpath, cache_path );
      struct stat statbuf;
      if ( stat ( buffer, &statbuf ) != 0 ) {
	snprintf ( buffer, PATH_MAX, "/bin/mkdir -p %s/%s", cacheoutpath, cache_path );
	system ( buffer );
      }
      // set up target filename to copy
      snprintf ( buffer, PATH_MAX, "%s/%s/%s.png", cacheoutpath, cache_path, app -> unique_id );
      pnd_log ( pndn_debug, "Found free space to cache preview to here: %s", buffer );
      if ( ! pnd_filecopy ( filepath, buffer ) ) {
	pnd_log ( pndn_error, "ERROR: Copying preview from %s to %s", filepath, buffer );
      }
    } else {
      pnd_log ( pndn_warning, "WARN: Couldn't find a device to cache preview to.\n" );
    }

  } // preview file cache

  // unmount it, if mounted
  if ( fullpath [ 0 ] ) {
    pnd_pnd_unmount ( pnd_run_script, fullpath, app -> unique_id );
  }

  //pnd_log ( pndn_debug, "Image size is %u x %u (max %u x %u)\n", s -> w, s -> h, maxwidth, maxheight );

  // scale
  if ( s -> w < maxwidth ) {
    // scale up?
    if ( pnd_conf_get_as_int_d ( g_conf, "previewpic.scale_up_bool", 1 ) ) {
      SDL_Surface *scaled;
      double scale = (double)maxwidth / (double)s -> w;
      //pnd_log ( pndn_debug, "  Upscaling; scale factor %f\n", scale );
      scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
      SDL_FreeSurface ( s );
      s = scaled;
    }
  } else if ( s -> w > maxwidth ) {
    SDL_Surface *scaled;
    double scale = (double)maxwidth / (double)s -> w;
    //pnd_log ( pndn_debug, "  Downscaling; scale factor %f\n", scale );
    scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
    SDL_FreeSurface ( s );
    s = scaled;
  }

  // add to cache
  c = (mm_cache_t*) malloc ( sizeof(mm_cache_t) );
  bzero ( c, sizeof(mm_cache_t) );

  if ( ! g_preview_cache ) {
    g_preview_cache = c;
  } else {
    c -> next = g_preview_cache;
    g_preview_cache = c;
  }

  strncpy ( c -> uniqueid, app -> unique_id, 1000 );
  c -> i = s;

  return ( 1 );
}
Exemplo n.º 2
0
void consume_configuration ( void ) {

  // attempt to fetch a sensible default searchpath for configs
  configpath = pnd_conf_query_searchpath();

  // attempt to fetch the apps config to pick up a searchpath
  pnd_conf_handle apph;

  apph = pnd_conf_fetch_by_id ( pnd_conf_apps, configpath );

  if ( apph ) {

    overridespath = pnd_conf_get_as_char ( apph, PND_PXML_OVERRIDE_KEY );

    if ( ! overridespath ) {
      overridespath = PND_PXML_OVERRIDE_SEARCHPATH;
    }

    notifypath = pnd_conf_get_as_char ( apph, PND_APPS_NOTIFY_KEY );

    if ( ! notifypath ) {
      notifypath = PND_APPS_NOTIFYPATH;
    }

  } else {
    // couldn't find a useful app search path so use the default
    overridespath = PND_PXML_OVERRIDE_SEARCHPATH;
    notifypath = PND_APPS_NOTIFYPATH;
  }

  // attempt to figure out where to drop dotfiles .. now that we're going
  // multi-target we see the limit of my rudimentary conf-file parser; should
  // just parse to an array of targets, rather that hardcoding two, but
  // on the other hand, don't likely see the need for more than two? (famous
  // last words.)
  pnd_conf_handle desktoph;

  desktoph = pnd_conf_fetch_by_id ( pnd_conf_desktop, configpath );

  // for 'desktop' main applications
  if ( desktoph ) {
    desktop_dotdesktoppath = pnd_conf_get_as_char ( desktoph, PND_DESKTOP_DOTDESKTOP_PATH_KEY );
    desktop_iconpath = pnd_conf_get_as_char ( desktoph, PND_DESKTOP_ICONS_PATH_KEY );
    desktop_appspath = pnd_conf_get_as_char ( desktoph, PND_DESKTOP_SEARCH_KEY );
    info_dotdesktoppath = pnd_conf_get_as_char ( desktoph, "info.dotdesktoppath" );
  }

  if ( ! desktop_dotdesktoppath ) {
    desktop_dotdesktoppath = PND_DESKTOP_DOTDESKTOP_PATH_DEFAULT;
  }

  if ( ! desktop_iconpath ) {
    desktop_iconpath = PND_DESKTOP_ICONS_PATH_DEFAULT;
  }

  if ( ! desktop_appspath ) {
    desktop_appspath = PND_DESKTOP_SEARCH_PATH_DEFAULT;
  }

  // for 'menu' applications
  if ( desktoph ) {
    menu_dotdesktoppath = pnd_conf_get_as_char ( desktoph, PND_MENU_DOTDESKTOP_PATH_KEY );
    menu_iconpath = pnd_conf_get_as_char ( desktoph, PND_MENU_ICONS_PATH_KEY );
    menu_appspath = pnd_conf_get_as_char ( desktoph, PND_MENU_SEARCH_KEY );
  }

  // info
  if ( desktoph ) {
    g_info_p = pnd_conf_get_as_int_d ( desktoph, "info.emit_info", 0 );
  }

  /* try to locate a runscript and optional hupscript
   */

  if ( apph ) {
    run_searchpath = pnd_conf_get_as_char ( apph, PND_PNDRUN_SEARCHPATH_KEY );
    run_script = pnd_conf_get_as_char ( apph, PND_PNDRUN_KEY );
    pndrun = NULL;

    if ( ! run_searchpath ) {
      run_searchpath = PND_APPS_SEARCHPATH;
      run_script = PND_PNDRUN_FILENAME;
    }

    if ( pnd_conf_get_as_int ( apph, PNDNOTIFYD_LOGLEVEL ) != PND_CONF_BADNUM ) {
      if ( pnd_log_do_buried_logging() == 0 ) {
	pnd_log_set_filter ( pnd_conf_get_as_int ( apph, PNDNOTIFYD_LOGLEVEL ) );
	pnd_log ( pndn_rem, "config file causes loglevel to change to %u", pnd_log_get_filter() );
      } else {
	pnd_log ( pndn_rem, "-l command line suppresses log level change in config file\n" );
      }
    }

  } else {
    run_searchpath = NULL;
    run_script = NULL;
    pndrun = PND_PNDRUN_DEFAULT;
  }

  if ( ! pndrun ) {
    pndrun = pnd_locate_filename ( run_searchpath, run_script );

    if ( pndrun ) {
      pndrun = strdup ( pndrun ); // so we don't just use the built in buffer; next locate will overwrite it
    } else {
      pndrun = PND_PNDRUN_DEFAULT;
    }

  }

  if ( desktoph && run_searchpath ) {
    char *t;

    if ( ( t = pnd_conf_get_as_char ( desktoph, PND_PNDHUP_KEY ) ) ) {
      pndhup = pnd_locate_filename ( run_searchpath, t );

      if ( pndhup ) {
	pndhup = strdup ( pndhup ); // so we don't just use the built in buffer; next locate will overwrite it
      }

#if 0 // don't enable this; if no key in config, we don't want to bother hupping
    } else {
      pndhup = pnd_locate_filename ( run_searchpath, PND_PNDHUP_FILENAME );
#endif
    }

  }

  // debug
  if ( run_searchpath ) pnd_log ( pndn_rem, "Locating pnd run in %s\n", run_searchpath );
  if ( run_script ) pnd_log ( pndn_rem, "Locating pnd runscript as %s\n", run_script );
  if ( pndrun ) pnd_log ( pndn_rem, "pndrun is %s\n", pndrun );
  if ( pndhup ) {
    pnd_log ( pndn_rem, "pndhup is %s\n", pndhup );
  } else {
    pnd_log ( pndn_rem, "No pndhup found (which is fine.)\n" );
  }

  /* cheap hack, maybe it'll help when pndnotifyd is coming up before the rest of the system and before
   * the user is formally logged in
   */
  pnd_log ( pndn_rem, "Setting a default $HOME to non-root user\n" );

  // first, try to see if known-username maps to a homedir; if so, just use that!
  // otherwise, pick first non-root homedir and assume .. or we start blaring .desktops
  // out to all users like idiots
  unsigned char got_user_homedir = 0;

  if ( g_username [ 0 ] ) {
    struct stat homedir;
    char path [ PATH_MAX ];

    sprintf ( path, "/home/%s", g_username );

    // does this made up path exist?
    if ( stat ( path, &homedir ) == 0 ) {

      // and its a dir?
      if ( S_ISDIR(homedir.st_mode) ) {
	pnd_log ( pndn_rem, "  User [%s] matches path [%s], going with '%s'\n", g_username, path, path );
	setenv ( "HOME", path, 1 /* overwrite */ );
	got_user_homedir = 1;
      } // and its a dir?

    } // guessing a homedirname..

  } // got a username?

  // if guessing a path was no good, just try finding one
  if ( got_user_homedir == 0 ) {
    DIR *dir;

    if ( ( dir = opendir ( "/home" ) ) ) {
      struct dirent *dirent;

      while ( ( dirent = readdir ( dir ) ) ) {
	pnd_log ( pndn_rem, "  Scanning user homedir '%s'\n", dirent -> d_name );

	// file is a .desktop?
	if ( dirent -> d_name [ 0 ] == '.' ) {
	  continue;
	} else if ( strcmp ( dirent -> d_name, "root" ) == 0 ) {
	  continue;
	}

	// a non-root user is found
	char buffer [ 200 ];
	sprintf ( buffer, "/home/%s", dirent -> d_name );
	pnd_log ( pndn_rem, "  Going with '%s'\n", buffer );
	setenv ( "HOME", buffer, 1 /* overwrite */ );
	break;

      } // while iterating through dir listing

      closedir ( dir );
    } // opendir?

  } // scope

  /* handle globbing or variable substitution
   */
  desktop_dotdesktoppath = pnd_expand_tilde ( strdup ( desktop_dotdesktoppath ) );
  desktop_iconpath = pnd_expand_tilde ( strdup ( desktop_iconpath ) );
  mkdir ( desktop_dotdesktoppath, 0777 );
  mkdir ( desktop_iconpath, 0777 );

  if ( info_dotdesktoppath ) {
    info_dotdesktoppath = pnd_expand_tilde ( strdup ( info_dotdesktoppath ) );
    mkdir ( info_dotdesktoppath, 0777 );
  }

  if ( menu_dotdesktoppath ) {
    menu_dotdesktoppath = pnd_expand_tilde ( strdup ( menu_dotdesktoppath ) );
    mkdir ( menu_dotdesktoppath, 0777 );
  }
  if ( menu_iconpath ) {
    menu_iconpath = pnd_expand_tilde ( strdup ( menu_iconpath ) );
    mkdir ( menu_iconpath, 0777 );
  }

  // done
  return;
}