2017-08-29 05:09:17 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-02-28 11:12:48 +00:00
|
|
|
"flag"
|
2017-08-29 14:05:51 +00:00
|
|
|
"log"
|
2017-08-29 05:09:17 +00:00
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/therecipe/qt/core"
|
|
|
|
"github.com/therecipe/qt/gui"
|
|
|
|
"github.com/therecipe/qt/qml"
|
|
|
|
"github.com/therecipe/qt/quickcontrols2"
|
|
|
|
|
2019-05-09 14:52:56 +00:00
|
|
|
gap "github.com/muesli/go-app-paths"
|
2019-05-09 14:33:26 +00:00
|
|
|
"github.com/muesli/telephant/accounts/mastodon"
|
2017-08-29 05:09:17 +00:00
|
|
|
)
|
|
|
|
|
2019-05-05 11:41:07 +00:00
|
|
|
var (
|
2020-02-28 11:12:48 +00:00
|
|
|
debug = flag.Bool("debug", true, "Enable debug output")
|
|
|
|
|
2019-05-15 04:49:45 +00:00
|
|
|
config Config
|
|
|
|
configFile string
|
|
|
|
|
2019-05-13 20:42:03 +00:00
|
|
|
notificationModel = NewMessageModel(nil)
|
2019-05-06 18:16:20 +00:00
|
|
|
conversationModel = NewMessageModel(nil)
|
|
|
|
accountMessagesModel = NewMessageModel(nil)
|
2019-05-19 06:08:40 +00:00
|
|
|
attachmentModel = NewAttachmentModel(nil)
|
2019-05-13 20:42:03 +00:00
|
|
|
paneModel = NewPaneModel(nil)
|
2019-05-05 11:41:07 +00:00
|
|
|
)
|
|
|
|
|
2019-05-13 20:42:03 +00:00
|
|
|
// closePane closes a pane
|
|
|
|
func closePane(idx int64) {
|
2020-02-28 11:12:48 +00:00
|
|
|
debugln("Closing pane", idx)
|
2019-05-13 20:42:03 +00:00
|
|
|
paneModel.RemovePane(int(idx))
|
|
|
|
}
|
|
|
|
|
2017-08-29 05:09:17 +00:00
|
|
|
// runApp loads and executes the QML UI
|
|
|
|
func runApp(config Config) {
|
2019-05-10 00:55:30 +00:00
|
|
|
var theme string
|
|
|
|
switch config.Theme {
|
|
|
|
case "System":
|
|
|
|
theme = ""
|
|
|
|
case "Light":
|
|
|
|
theme = "Default"
|
|
|
|
default:
|
|
|
|
theme = config.Theme
|
|
|
|
}
|
|
|
|
if theme != "" {
|
|
|
|
quickcontrols2.QQuickStyle_SetStyle(theme)
|
|
|
|
}
|
2017-08-29 05:09:17 +00:00
|
|
|
|
|
|
|
app := qml.NewQQmlApplicationEngine(nil)
|
|
|
|
app.RootContext().SetContextProperty("uiBridge", uiBridge)
|
|
|
|
app.RootContext().SetContextProperty("accountBridge", accountBridge)
|
2019-05-06 18:16:20 +00:00
|
|
|
app.RootContext().SetContextProperty("profileBridge", profileBridge)
|
2017-08-29 05:09:17 +00:00
|
|
|
app.RootContext().SetContextProperty("settings", configBridge)
|
|
|
|
|
2019-05-09 14:33:26 +00:00
|
|
|
app.Load(core.NewQUrl3("qrc:/qml/telephant.qml", 0))
|
2017-08-29 05:09:17 +00:00
|
|
|
gui.QGuiApplication_Exec()
|
|
|
|
}
|
|
|
|
|
2019-05-01 15:15:56 +00:00
|
|
|
// setupMastodon starts a new Mastodon client and sets up event handling & models for it
|
|
|
|
func setupMastodon(config Account) {
|
2019-05-05 11:41:07 +00:00
|
|
|
tc = mastodon.NewAccount(config.Instance, config.Token, config.ClientID, config.ClientSecret)
|
2019-05-02 08:37:04 +00:00
|
|
|
postModel := NewMessageModel(nil)
|
2019-05-14 02:20:27 +00:00
|
|
|
|
|
|
|
// Notifications model must the first model to be added
|
|
|
|
// It will always be displayed right-most
|
2019-05-15 03:03:15 +00:00
|
|
|
paneModel.clear()
|
2019-05-13 20:42:03 +00:00
|
|
|
{
|
|
|
|
var pane = NewPane(nil)
|
2020-03-03 04:16:35 +00:00
|
|
|
pane.ID = "notifications"
|
2019-05-14 02:20:27 +00:00
|
|
|
pane.Name = "Notifications"
|
|
|
|
pane.Sticky = true
|
2020-03-03 05:30:33 +00:00
|
|
|
pane.Default = true
|
2019-05-14 02:20:27 +00:00
|
|
|
pane.Model = notificationModel
|
2019-05-13 20:42:03 +00:00
|
|
|
paneModel.AddPane(pane)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
var pane = NewPane(nil)
|
2020-03-03 04:16:35 +00:00
|
|
|
pane.ID = "home"
|
2019-05-14 02:20:27 +00:00
|
|
|
pane.Name = "Messages"
|
2020-03-03 05:30:33 +00:00
|
|
|
pane.Default = true
|
2019-05-14 02:20:27 +00:00
|
|
|
pane.Model = postModel
|
2019-05-13 20:42:03 +00:00
|
|
|
paneModel.AddPane(pane)
|
|
|
|
}
|
2017-08-29 05:09:17 +00:00
|
|
|
|
2019-05-15 00:35:20 +00:00
|
|
|
panes := tc.Panes()
|
|
|
|
for _, p := range panes {
|
2020-03-03 05:30:33 +00:00
|
|
|
if !p.Default {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-05-15 00:35:20 +00:00
|
|
|
model := NewMessageModel(nil)
|
|
|
|
evchan := make(chan interface{})
|
|
|
|
|
|
|
|
go handleEvents(evchan, model)
|
|
|
|
p.Stream(evchan)
|
|
|
|
|
|
|
|
var pane = NewPane(nil)
|
2020-03-03 04:16:35 +00:00
|
|
|
pane.ID = p.ID
|
2019-05-15 00:35:20 +00:00
|
|
|
pane.Name = p.Title
|
2020-03-03 05:30:33 +00:00
|
|
|
pane.Default = p.Default
|
2019-05-15 00:35:20 +00:00
|
|
|
pane.Model = model
|
|
|
|
paneModel.AddPane(pane)
|
|
|
|
}
|
2017-08-29 05:09:17 +00:00
|
|
|
|
|
|
|
evchan := make(chan interface{})
|
2019-05-13 20:42:03 +00:00
|
|
|
go handleEvents(evchan, postModel)
|
2019-05-10 13:47:17 +00:00
|
|
|
go tc.Run(evchan)
|
2017-08-29 05:09:17 +00:00
|
|
|
}
|
|
|
|
|
2020-02-28 11:12:48 +00:00
|
|
|
func debugln(s ...interface{}) {
|
|
|
|
if *debug {
|
|
|
|
log.Println(s...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-29 05:09:17 +00:00
|
|
|
func main() {
|
2020-02-28 11:12:48 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
2019-05-09 14:33:26 +00:00
|
|
|
core.QCoreApplication_SetApplicationName("Telephant")
|
2019-05-01 15:15:56 +00:00
|
|
|
core.QCoreApplication_SetOrganizationName("fribbledom.com")
|
2017-08-29 05:09:17 +00:00
|
|
|
core.QCoreApplication_SetAttribute(core.Qt__AA_EnableHighDpiScaling, true)
|
|
|
|
|
2019-05-11 00:14:35 +00:00
|
|
|
ga := gui.NewQGuiApplication(len(os.Args), os.Args)
|
|
|
|
ga.SetWindowIcon(gui.NewQIcon5(":/qml/images/telephant_logo.png"))
|
2017-08-29 05:09:17 +00:00
|
|
|
setupQmlBridges()
|
|
|
|
|
|
|
|
// load config
|
2020-04-04 06:44:50 +00:00
|
|
|
scope := gap.NewScope(gap.User, "telephant")
|
2019-05-09 14:52:56 +00:00
|
|
|
configDir, err := scope.ConfigPath("")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
os.MkdirAll(configDir, 0700)
|
|
|
|
|
2019-05-15 04:49:45 +00:00
|
|
|
configFile, err = scope.ConfigPath("telephant.conf")
|
2019-05-11 02:01:50 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-05-09 14:52:56 +00:00
|
|
|
config = LoadConfig(configFile)
|
2019-05-10 00:55:30 +00:00
|
|
|
if config.Theme == "" {
|
|
|
|
config.Theme = "Material"
|
|
|
|
}
|
2017-08-29 05:09:17 +00:00
|
|
|
if config.Style == "" {
|
2019-05-10 00:55:30 +00:00
|
|
|
config.Style = "Dark"
|
2017-08-29 05:09:17 +00:00
|
|
|
}
|
2020-04-20 18:06:19 +00:00
|
|
|
if config.FontFamily == "" {
|
|
|
|
config.FontFamily = "Noto Sans"
|
|
|
|
}
|
|
|
|
if config.EmojiFont == "" {
|
|
|
|
config.EmojiFont = "Noto Color Emoji"
|
|
|
|
}
|
2019-05-10 00:55:30 +00:00
|
|
|
configBridge.SetTheme(config.Theme)
|
2017-08-29 05:09:17 +00:00
|
|
|
configBridge.SetStyle(config.Style)
|
2019-05-10 13:47:17 +00:00
|
|
|
configBridge.SetFirstRun(config.FirstRun)
|
2019-05-19 02:33:37 +00:00
|
|
|
configBridge.SetPositionX(config.PositionX)
|
|
|
|
configBridge.SetPositionY(config.PositionY)
|
|
|
|
configBridge.SetWidth(config.Width)
|
|
|
|
configBridge.SetHeight(config.Height)
|
2020-04-20 18:06:19 +00:00
|
|
|
configBridge.SetFontfamily(config.FontFamily)
|
|
|
|
configBridge.SetEmojifont(config.EmojiFont)
|
2017-08-29 05:09:17 +00:00
|
|
|
|
2020-04-06 04:58:30 +00:00
|
|
|
accountBridge.SetUsername("Not connected...")
|
|
|
|
accountBridge.SetNotifications(notificationModel)
|
|
|
|
accountBridge.SetAttachments(attachmentModel)
|
|
|
|
accountBridge.SetConversation(conversationModel)
|
|
|
|
accountBridge.SetAccountMessages(accountMessagesModel)
|
|
|
|
accountBridge.SetAvatar("qrc:/qml/images/telephant_logo.png")
|
|
|
|
accountBridge.SetPosts(0)
|
|
|
|
accountBridge.SetFollowCount(0)
|
|
|
|
accountBridge.SetFollowerCount(0)
|
|
|
|
accountBridge.SetPostSizeLimit(0)
|
|
|
|
accountBridge.SetPanes(paneModel)
|
|
|
|
|
2020-03-04 12:24:05 +00:00
|
|
|
if len(config.Account) > 0 {
|
|
|
|
setupMastodon(config.Account[0])
|
|
|
|
}
|
2017-08-29 05:09:17 +00:00
|
|
|
runApp(config)
|
|
|
|
|
|
|
|
// save config
|
2019-05-10 00:55:30 +00:00
|
|
|
config.Theme = configBridge.Theme()
|
2017-08-29 05:09:17 +00:00
|
|
|
config.Style = configBridge.Style()
|
2019-05-19 02:33:37 +00:00
|
|
|
config.PositionX = configBridge.PositionX()
|
|
|
|
config.PositionY = configBridge.PositionY()
|
|
|
|
config.Width = configBridge.Width()
|
|
|
|
config.Height = configBridge.Height()
|
2019-05-10 13:47:17 +00:00
|
|
|
config.FirstRun = false
|
2020-04-20 18:06:19 +00:00
|
|
|
config.FontFamily = configBridge.Fontfamily()
|
|
|
|
config.EmojiFont = configBridge.Emojifont()
|
2019-05-09 14:52:56 +00:00
|
|
|
SaveConfig(configFile, config)
|
2017-08-29 05:09:17 +00:00
|
|
|
}
|