/**
  * Recuperer tous les parametres d'une configuration utilisateur.
  * @param string $path Le chemin vers le fichier de configuration utilisateur.
  * @param string $base Le chemin vers le fichier servant de modele si le fichier de configuration utilisateur n'existe pas.
  */
 protected function getUserConfig($path, $base)
 {
     $config = new Config($this->webos);
     $data = array();
     if (is_array($base)) {
         $data = $base;
         $base = null;
         foreach ($data as $attribute => $value) {
             $config->set($attribute, $value);
         }
     }
     if (!$this->webos->getUser()->isConnected()) {
         if (!empty($base)) {
             $config->load($base);
             $data = $config->getConfig();
         }
     } else {
         $authorisations = $this->webos->getAuthorization();
         $readAuthorisation = $authorisations->getArgumentAuthorizations($path, 'file', 'read');
         $writeAuthorisation = $authorisations->getArgumentAuthorizations($path, 'file', 'write');
         if ($authorisations->can($writeAuthorisation)) {
             if (!$this->webos->managers()->get('File')->exists($path)) {
                 if (strpos($path, '/', 1) !== false) {
                     $dirname = preg_replace('#/[^/]*/?$#', '', $path);
                 } else {
                     if (strpos($path, '/') === 0) {
                         $dirname = '/';
                     }
                 }
                 $canCreateFile = true;
                 if (!$this->webos->managers()->get('File')->exists($dirname)) {
                     if ($authorisations->can($authorisations->getArgumentAuthorizations($dirname, 'file', 'write'))) {
                         $this->webos->managers()->get('File')->createDirRecursive($dirname);
                     } else {
                         $canCreateFile = false;
                     }
                 }
                 if ($canCreateFile) {
                     if (!empty($base)) {
                         $default = $this->webos->managers()->get('File')->get($base);
                         $default->copy($path);
                     } else {
                         $file = $this->webos->managers()->get('File')->createFile($path);
                         $file->setContents($config->saveXML());
                     }
                 }
             }
             if ($this->webos->managers()->get('File')->exists($path) && $authorisations->can($readAuthorisation)) {
                 $config->load($path);
                 $data = $config->getConfig();
             } else {
                 if (!empty($base)) {
                     $config->load($base);
                     $data = $config->getConfig();
                 }
             }
         } else {
             if (!empty($base)) {
                 $config->load($base);
                 $data = $config->getConfig();
             }
         }
     }
     return $data;
 }
 /**
  * Recuperer tous les raccourcis.
  */
 protected function get()
 {
     if ($this->webos->getUser()->isConnected()) {
         $path = '~/.config/prefered-openers.xml';
         $config = new Config($this->webos);
         if (!$this->webos->managers()->get('File')->exists($path)) {
             $file = $this->webos->managers()->get('File')->createFile($path);
             $file->setContents($config->saveXML());
         }
         $config->load($path);
         $preferedOpeners = $config->getConfig();
     } else {
         $preferedOpeners = array();
     }
     //On recupere la liste des raccourcis
     $applications = $this->webos->managers()->get('File')->get('/usr/share/applications/')->contents();
     //On initialise la liste
     $list = array('applications' => array(), 'categories' => array());
     //On recupere les infos pour chaque raccourci
     foreach ($applications as $shortcut) {
         if ($shortcut->isDir()) {
             continue;
         }
         if ($shortcut->extension() != 'xml') {
             continue;
         }
         $xml = new \DOMDocument();
         $xml->loadXML($shortcut->contents());
         $attributes = $xml->getElementsByTagName('attribute');
         $attributesList = array();
         foreach ($attributes as $attribute) {
             $attributesList[$attribute->getAttribute('name')] = $attribute->getAttribute('value');
         }
         $attributesList['prefered_open'] = '';
         if (array_key_exists($shortcut->filename(), $preferedOpeners)) {
             $opens = $preferedOpeners[$shortcut->filename()];
             $attributesList['prefered_open'] = $opens;
             if (array_key_exists('open', $attributesList)) {
                 $attributesList['open'] .= ',' . $opens;
             } else {
                 $attributesList['open'] = $opens;
             }
         }
         if (array_key_exists('visible', $attributesList) && (int) $attributesList['visible'] == 0) {
             continue;
         }
         $list['applications'][$shortcut->filename()] = $attributesList;
     }
     $favoritesConfig = $this->_getFavoritesConfig();
     foreach ($favoritesConfig->getConfig() as $app => $value) {
         if ((int) $value > 0 && array_key_exists($app, $list['applications'])) {
             $list['applications'][$app]['favorite'] = (int) $value;
         }
     }
     //On trie le tableau
     uasort($list['applications'], function ($a, $b) {
         $specialChars = array('Š' => 'S', 'š' => 's', 'Ð' => 'Dj', 'Ž' => 'Z', 'ž' => 'z', 'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä' => 'A', 'Å' => 'A', 'Æ' => 'A', 'Ç' => 'C', 'È' => 'E', 'É' => 'E', 'Ê' => 'E', 'Ë' => 'E', 'Ì' => 'I', 'Í' => 'I', 'Î' => 'I', 'Ï' => 'I', 'Ñ' => 'N', 'Ò' => 'O', 'Ó' => 'O', 'Ô' => 'O', 'Õ' => 'O', 'Ö' => 'O', 'Ø' => 'O', 'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U', 'Ü' => 'U', 'Ý' => 'Y', 'Þ' => 'B', 'ß' => 'Ss', 'à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a', 'ä' => 'a', 'å' => 'a', 'æ' => 'a', 'ç' => 'c', 'è' => 'e', 'é' => 'e', 'ê' => 'e', 'ë' => 'e', 'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i', 'ð' => 'o', 'ñ' => 'n', 'ò' => 'o', 'ó' => 'o', 'ô' => 'o', 'õ' => 'o', 'ö' => 'o', 'ø' => 'o', 'ù' => 'u', 'ú' => 'u', 'û' => 'u', 'ý' => 'y', 'ý' => 'y', 'þ' => 'b', 'ÿ' => 'y', 'ƒ' => 'f');
         $aTitle = strtr($a['title'], $specialChars);
         $bTitle = strtr($b['title'], $specialChars);
         return strcmp($aTitle, $bTitle);
     });
     //On recupere la liste des categories
     $categories = $this->webos->managers()->get('File')->get('/usr/share/categories/')->contents();
     //On recupere les infos pour cahque categorie
     foreach ($categories as $shortcut) {
         if ($shortcut->isDir()) {
             continue;
         }
         if ($shortcut->extension() != 'xml') {
             continue;
         }
         $xml = new \DOMDocument();
         $xml->loadXML($shortcut->contents());
         $attributes = $xml->getElementsByTagName('attribute');
         $attributesList = array();
         foreach ($attributes as $attribute) {
             $attributesList[$attribute->getAttribute('name')] = $attribute->getAttribute('value');
         }
         if (array_key_exists('visible', $attributesList) && (int) $attributesList['visible'] == 0) {
             continue;
         }
         $list['categories'][] = $attributesList;
     }
     $this->webos->getHTTPResponse()->setData($list);
 }