예제 #1
0
QString copyDesktopFile(const KURL& url)
{
   QString file = newDesktopFile(url);
   KURL dest;
   dest.setPath(file);
   KIO::NetAccess::upload(url.path(), dest, 0);
   return file;
}
/**
 *  Initialize the applet.
 *
 *  Only called when we create a new applet. Asks the user for an URL and
 *  creates a .desktop file...
 */
bool LauncherApplet::init()
{
    //    
    // Get an url...
    //
    KFileDialog fileDlg(0, 0, 0, 0, true);
    fileDlg.exec();
    _url = fileDlg.selectedURL();
    
    if (!_url.isValid()) {
        return false;
    }
    
    //
    // Create a .desktop file
    //
    QString file = newDesktopFile(_url);
    
    if (KDesktopFile::isDesktopFile(_url.path())) {
        // Copy existing file...
        KURL dest;
        dest.setPath(file);
        KIO::NetAccess::upload(_url.path(), dest, 0);
    } else {
        // Create new file...
        KDesktopFile df(file);
        
        df.writeEntry("Encoding", "UTF-8");
        df.writeEntry("Type","Link");
        df.writeEntry("Name", _url.prettyURL());
        
        if (_url.isLocalFile()) {
            KFileItem item( KFileItem::Unknown, KFileItem::Unknown, _url );
            df.writeEntry("Icon", item.iconName() );
        } else {
            df.writeEntry("Icon", KMimeType::favIconForURL(_url));
        }
        df.writeEntry("URL", _url.url());
        df.sync();
    }
    
    // Use the .desktop file as URL
    _url = KURL();
    _url.setPath(file);
    
    // Update everything...
    slotSettingsChanged();
    
    return true;
}