1 /*
2  * ProjectsSettings.hpp
3  *
4  * Copyright (C) 2021 by RStudio, PBC
5  *
6  * Unless you have received this program directly from RStudio pursuant
7  * to the terms of a commercial license agreement with RStudio, then
8  * this program is licensed to you under the terms of version 3 of the
9  * GNU Affero General Public License. This program is distributed WITHOUT
10  * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
11  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
12  * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
13  *
14  */
15 
16 #ifndef SESSION_SESSION_PROJECTS_SETTINGS_HPP
17 #define SESSION_SESSION_PROJECTS_SETTINGS_HPP
18 
19 // header-only file for access to projects settings from many contexts
20 // (main session thread, background threads, other processes, etc.)
21 
22 #include <core/SharedSettings.hpp>
23 
24 #include <core/r_util/RSessionContext.hpp>
25 
26 #define kProjectsSettings              "projects_settings"
27 #define kNextSessionProject            "next-session-project"
28 #define kSwitchToProject               "switch-to-project"
29 #define kLastProjectPath               "last-project-path"
30 #define kAlwaysRestoreLastProject      "restoreLastProject"
31 
32 namespace rstudio {
33 namespace session {
34 namespace projects {
35 
36 class ProjectsSettings : public core::SharedSettings
37 {
38 public:
ProjectsSettings(const core::FilePath & userScratchPath)39    explicit ProjectsSettings(const core::FilePath& userScratchPath)
40       : core::SharedSettings(projectsSettingsPath(userScratchPath))
41    {
42    }
43 
nextSessionProject() const44    std::string nextSessionProject() const
45    {
46       return readSetting(kNextSessionProject);
47    }
48 
setNextSessionProject(const std::string & nextSessionProject)49    void setNextSessionProject(const std::string& nextSessionProject)
50    {
51       writeSetting(kNextSessionProject, nextSessionProject);
52    }
53 
switchToProjectPath() const54    std::string switchToProjectPath() const
55    {
56       return readSetting(kSwitchToProject);
57    }
58 
setSwitchToProjectPath(const std::string & switchToProjectPath)59    void setSwitchToProjectPath(const std::string& switchToProjectPath)
60    {
61       writeSetting(kSwitchToProject, switchToProjectPath);
62    }
63 
lastProjectPath() const64    core::FilePath lastProjectPath() const
65    {
66       std::string path = readSetting(kLastProjectPath);
67       if (!path.empty())
68          return core::FilePath(path);
69       else
70          return core::FilePath();
71    }
72 
setLastProjectPath(const core::FilePath & lastProjectPath)73    void setLastProjectPath(const core::FilePath& lastProjectPath)
74    {
75       if (!lastProjectPath.isEmpty())
76          writeSetting(kLastProjectPath, lastProjectPath.getAbsolutePath());
77       else
78          writeSetting(kLastProjectPath, "");
79    }
80 
81 private:
82 
projectsSettingsPath(const core::FilePath & userScratchPath)83    static core::FilePath projectsSettingsPath(
84                      const core::FilePath& userScratchPath)
85    {
86       using namespace rstudio::core;
87       FilePath settingsPath = userScratchPath.completePath(kProjectsSettings);
88       Error error = settingsPath.ensureDirectory();
89       if (error)
90          LOG_ERROR(error);
91 
92       return settingsPath;
93    }
94 };
95 
96 
97 } // namepspace projects
98 } // namespace session
99 } // namespace rstudio
100 
101 #endif // SESSION_SESSION_PROJECTS_SETTINGS_HPP
102 
103