@ -6,7 +6,6 @@
*/
namespace OC\App;
use InvalidArgumentException;
use OC\AppConfig;
use OC\AppFramework\Bootstrap\Coordinator;
use OC\ServerNotAvailableException;
@ -24,6 +23,7 @@ use OCP\ICacheFactory;
use OCP\IConfig;
use OCP\IGroup;
use OCP\IGroupManager;
use OCP\INavigationManager;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserSession;
@ -67,6 +67,7 @@ class AppManager implements IAppManager {
private ?AppConfig $appConfig = null;
private ?IURLGenerator $urlGenerator = null;
private ?INavigationManager $navigationManager = null;
/**
* Be extremely careful when injecting classes here. The AppManager is used by the installer,
@ -82,6 +83,13 @@ class AppManager implements IAppManager {
) {
}
private function getNavigationManager(): INavigationManager {
if ($this->navigationManager === null) {
$this->navigationManager = \OCP\Server::get(INavigationManager::class);
}
return $this->navigationManager;
}
public function getAppIcon(string $appId, bool $dark = false): ?string {
$possibleIcons = $dark ? [$appId . '-dark.svg', 'app-dark.svg'] : [$appId . '.svg', 'app.svg'];
$icon = null;
@ -820,60 +828,42 @@ class AppManager implements IAppManager {
return $this->defaultEnabled;
}
/**
* @inheritdoc
*/
public function getDefaultAppForUser(?IUser $user = null, bool $withFallbacks = true): string {
// Set fallback to always-enabled files app
$appId = $withFallbacks ? 'files' : '';
$defaultApps = explode(',', $this->config->getSystemValueString('defaultapp', ''));
$defaultApps = array_filter($defaultApps);
$user ??= $this->userSession->getUser();
if ($user !== null) {
$userDefaultApps = explode(',', $this->config->getUserValue($user->getUID(), 'core', 'defaultapp'));
$defaultApps = array_filter(array_merge($userDefaultApps, $defaultApps));
if (empty($defaultApps) & & $withFallbacks) {
/* Fallback on user defined apporder */
$customOrders = json_decode($this->config->getUserValue($user->getUID(), 'core', 'apporder', '[]'), true, flags:JSON_THROW_ON_ERROR);
if (!empty($customOrders)) {
// filter only entries with app key (when added using closures or NavigationManager::add the app is not guranteed to be set)
$customOrders = array_filter($customOrders, fn ($entry) => isset($entry['app']));
// sort apps by order
usort($customOrders, fn ($a, $b) => $a['order'] - $b['order']);
// set default apps to sorted apps
$defaultApps = array_map(fn ($entry) => $entry['app'], $customOrders);
}
}
}
if (empty($defaultApps) & & $withFallbacks) {
$defaultApps = ['dashboard','files'];
}
// Find the first app that is enabled for the current user
foreach ($defaultApps as $defaultApp) {
$defaultApp = \OC_App::cleanAppId(strip_tags($defaultApp));
if ($this->isEnabledForUser($defaultApp, $user)) {
$appId = $defaultApp;
break;
}
}
return $appId;
$id = $this->getNavigationManager()->getDefaultEntryIdForUser($user, $withFallbacks);
$entry = $this->getNavigationManager()->get($id);
return (string)$entry['app'];
}
/**
* @inheritdoc
*/
public function getDefaultApps(): array {
return explode(',', $this->config->getSystemValueString('defaultapp', 'dashboard,files'));
$ids = $this->getNavigationManager()->getDefaultEntryIds();
return array_values(array_unique(array_map(function (string $id) {
$entry = $this->getNavigationManager()->get($id);
return (string)$entry['app'];
}, $ids)));
}
/**
* @inheritdoc
*/
public function setDefaultApps(array $defaultApps): void {
foreach ($defaultApps as $app) {
if (!$this->isInstalled($app)) {
$this->logger->debug('Can not set not installed app as default app', ['missing_app' => $app]);
throw new InvalidArgumentException('App is not installed');
$entries = $this->getNavigationManager()->getAll();
$ids = [];
foreach ($defaultApps as $defaultApp) {
foreach ($entries as $entry) {
if ((string)$entry['app'] === $defaultApp) {
$ids[] = (string)$entry['id'];
break;
}
}
}
$this->config->setSystemValue('defaultapp', join(',', $defaultApps));
$this->getNavigationManager()->setDefaultEntryIds($ids);
}
public function isBackendRequired(string $backend): bool {