__Use case:__ I have a plugin that generates a project file based on user input and afterwards I would like to have it ask the user if they want to open the project in the current Geany instance. You can view, comment on, or merge this pull request online at:
https://github.com/geany/geany/pull/1222
-- Commit Summary --
* Add new API function to open a project file
-- File Changes --
M src/plugindata.h (2) M src/project.c (22) M src/project.h (2)
-- Patch Links --
https://github.com/geany/geany/pull/1222.patch https://github.com/geany/geany/pull/1222.diff
LGBI
Ammended commit to replace `Project->New` with `Project->Open` in doc-comment only.
Ammended commit to change API 229 to 230 since 094737533609dff5a2ff101f97e8c156bcca4979.
Do you have an actual plugin that needs this or is this just for geany++?
Yes, I have a plugin that generates a Geany++ plugin (ex. the Autotools files, a simple plugin C++ file, etc). It has an option to generate a Geany project file, and if chosen, after the generation is complete it offers to open the Geany project to start working on the plugin.
If there are no objections, I will merge this "soon".
b4n commented on this pull request.
Looks mostly fine, see comments.
- This is done as if the user had done it using the `Project->Open`
+ * menu item (ie. it will confirm closing the current project). Any + * files in the project's session will be re-opened. + * + * @param project_locale_fn The filename of the project file in locale + * filename encoding. + * + * @return @c TRUE if the project was opened, @c FALSE otherwise. + * + * @since 1.29 (API 230) + */ +GEANY_API_SYMBOL +gboolean project_open_file(const gchar *project_locale_fn) +{ + return (project_ask_close() && + project_load_file_with_session(project_locale_fn));
It might make sense to perform the same checks as `osx.c` does, that is that the project file to open is not obviously the same as the currently open one:
```C if (app->project == NULL || (g_strcmp0(utf8_path, app->project->file_name) != 0 && project_ask_close())) project_load_file_with_session(locale_path); ```
+/**
+ * Open the given project file. + * + * This is done as if the user had done it using the `Project->Open` + * menu item (ie. it will confirm closing the current project). Any + * files in the project's session will be re-opened. + * + * @param project_locale_fn The filename of the project file in locale + * filename encoding. + * + * @return @c TRUE if the project was opened, @c FALSE otherwise. + * + * @since 1.29 (API 230) + */ +GEANY_API_SYMBOL +gboolean project_open_file(const gchar *project_locale_fn)
This new API should probably be used in `osx.c`'s `open_project_idle()` and in `ui_utils.c`'s `recent_project_activate_cb()`.
codebrainz commented on this pull request.
- This is done as if the user had done it using the `Project->Open`
+ * menu item (ie. it will confirm closing the current project). Any + * files in the project's session will be re-opened. + * + * @param project_locale_fn The filename of the project file in locale + * filename encoding. + * + * @return @c TRUE if the project was opened, @c FALSE otherwise. + * + * @since 1.29 (API 230) + */ +GEANY_API_SYMBOL +gboolean project_open_file(const gchar *project_locale_fn) +{ + return (project_ask_close() && + project_load_file_with_session(project_locale_fn));
Yeah, it might not be a bad idea. I don't think that check is correct though. On Windows it should use `g_utf8_casefold()` because filenames are not case sensitive and on all platforms it should probably use `g_utf8_collate_key_for_filename()` or similar to do a closer to "proper" comparison.
b4n commented on this pull request.
- This is done as if the user had done it using the `Project->Open`
+ * menu item (ie. it will confirm closing the current project). Any + * files in the project's session will be re-opened. + * + * @param project_locale_fn The filename of the project file in locale + * filename encoding. + * + * @return @c TRUE if the project was opened, @c FALSE otherwise. + * + * @since 1.29 (API 230) + */ +GEANY_API_SYMBOL +gboolean project_open_file(const gchar *project_locale_fn) +{ + return (project_ask_close() && + project_load_file_with_session(project_locale_fn));
I don't think that check is correct though. On Windows it should use `g_utf8_casefold()` because filenames are not case sensitive
Yeah probably.
and on all platforms it should probably use `g_utf8_collate_key_for_filename()` or similar to do a closer to "proper" comparison.
No, on non-Windows, filenames are byte sequences generally; and anyway `g_utf8_collate_key_for_filename()` is meant for sorting filenames naturally (e.g. case insensitive, human-readbale numbers, etc.), not compare for path equality.
codebrainz commented on this pull request.
- This is done as if the user had done it using the `Project->Open`
+ * menu item (ie. it will confirm closing the current project). Any + * files in the project's session will be re-opened. + * + * @param project_locale_fn The filename of the project file in locale + * filename encoding. + * + * @return @c TRUE if the project was opened, @c FALSE otherwise. + * + * @since 1.29 (API 230) + */ +GEANY_API_SYMBOL +gboolean project_open_file(const gchar *project_locale_fn) +{ + return (project_ask_close() && + project_load_file_with_session(project_locale_fn));
No, on non-Windows, filenames are byte sequences generally
Then why comparing those bytes after they're converted to UTF-8? I don't think it's "correct" like this, though probably not a big deal.
elextr commented on this pull request.
- This is done as if the user had done it using the `Project->Open`
+ * menu item (ie. it will confirm closing the current project). Any + * files in the project's session will be re-opened. + * + * @param project_locale_fn The filename of the project file in locale + * filename encoding. + * + * @return @c TRUE if the project was opened, @c FALSE otherwise. + * + * @since 1.29 (API 230) + */ +GEANY_API_SYMBOL +gboolean project_open_file(const gchar *project_locale_fn) +{ + return (project_ask_close() && + project_load_file_with_session(project_locale_fn));
Why bother comparing the paths? If the user asks for it, do it. If there is some technical problem about closing and immediately re-opening the same one then it needs to be fixed because links mean it can happen with different paths.
b4n commented on this pull request.
- This is done as if the user had done it using the `Project->Open`
+ * menu item (ie. it will confirm closing the current project). Any + * files in the project's session will be re-opened. + * + * @param project_locale_fn The filename of the project file in locale + * filename encoding. + * + * @return @c TRUE if the project was opened, @c FALSE otherwise. + * + * @since 1.29 (API 230) + */ +GEANY_API_SYMBOL +gboolean project_open_file(const gchar *project_locale_fn) +{ + return (project_ask_close() && + project_load_file_with_session(project_locale_fn));
Well maybe it doesn't matter much and maybe should not be in the generic function, but e.g. for OSX opening I would think it indeed makes sense not to ask again if the user wants to open the already opened project: raising the window is good enough to do what the user wanted, and closing and reopening the project will just lose the undo buffers and alike for no reason.
elextr commented on this pull request.
- This is done as if the user had done it using the `Project->Open`
+ * menu item (ie. it will confirm closing the current project). Any + * files in the project's session will be re-opened. + * + * @param project_locale_fn The filename of the project file in locale + * filename encoding. + * + * @return @c TRUE if the project was opened, @c FALSE otherwise. + * + * @since 1.29 (API 230) + */ +GEANY_API_SYMBOL +gboolean project_open_file(const gchar *project_locale_fn) +{ + return (project_ask_close() && + project_load_file_with_session(project_locale_fn));
Ok, losing the undo is reason enough to avoid it in the case of accidental wrong selection by the user.
github-comments@lists.geany.org